CMS 3D CMS Logo

GeometryInterface.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: SiPixelPhase1Common
4 // Class: GeometryInterface
5 //
6 // Geometry depedence goes here.
7 //
8 // Original Author: Marcel Schneider
9 
11 
12 // general plotting helpers
14 
15 // edm stuff
18 
19 // Tracker Geometry/Topology stuff
30 
31 // Pixel names
34 
35 // C++ stuff
36 #include <cassert>
37 #include <cstdint>
38 #include <iostream>
39 #include <iomanip>
40 #include <memory>
41 
43 
45  //loadFromAlignment(iSetup, iConfig);
46  loadFromTopology(iSetup, iConfig);
47  loadTimebased(iSetup, iConfig);
48  loadModuleLevel(iSetup, iConfig);
49  loadFEDCabling(iSetup, iConfig);
51  edm::LogInfo log("GeometryInterface");
52  log << "Known colum names:\n";
53  for (auto e : ids)
54  log << "+++ column: " << e.first << " ok " << bool(extractors[e.second]) << " min " << min_value[e.second]
55  << " max " << max_value[e.second] << "\n";
56  is_loaded = true;
57 }
58 
60  // Get a Topology
61  edm::ESHandle<TrackerTopology> trackerTopologyHandle;
62  iSetup.get<TrackerTopologyRcd>().get(trackerTopologyHandle);
63  assert(trackerTopologyHandle.isValid());
64 
65  std::vector<ID> geomquantities;
66 
67  struct TTField {
68  const TrackerTopology* tt;
70  Value operator()(InterestingQuantities const& iq) {
71  if (tt->hasField(iq.sourceModule, field))
72  return tt->getField(iq.sourceModule, field);
73  else
74  return UNDEFINED;
75  };
76  };
77 
78  const TrackerTopology* tt = trackerTopologyHandle.operator->();
79 
80  std::vector<std::pair<std::string, TTField>> namedPartitions{
81  {"PXEndcap", {tt, TrackerTopology::PFSide}},
82 
83  {"PXLayer", {tt, TrackerTopology::PBLayer}},
84  {"PXLadder", {tt, TrackerTopology::PBLadder}},
85  {"PXBModule", {tt, TrackerTopology::PBModule}},
86 
87  {"PXBlade", {tt, TrackerTopology::PFBlade}},
88  {"PXDisk", {tt, TrackerTopology::PFDisk}},
89  {"PXPanel", {tt, TrackerTopology::PFPanel}},
90  {"PXFModule", {tt, TrackerTopology::PFModule}},
91  };
92 
93  for (auto& e : namedPartitions) {
94  geomquantities.push_back(intern(e.first));
95  addExtractor(intern(e.first), e.second, UNDEFINED, UNDEFINED);
96  }
97 
98  auto pxbarrel = [](InterestingQuantities const& iq) {
99  return iq.sourceModule.subdetId() == PixelSubdetector::PixelBarrel ? 0 : UNDEFINED;
100  };
101  auto pxforward = [](InterestingQuantities const& iq) {
102  return iq.sourceModule.subdetId() == PixelSubdetector::PixelEndcap ? 0 : UNDEFINED;
103  };
104  auto pxall = [](InterestingQuantities const& iq) { return 0; };
105  addExtractor(intern("PXBarrel"), pxbarrel, 0, 0);
106  addExtractor(intern("PXForward"), pxforward, 0, 0);
107  addExtractor(intern("PXAll"), pxall, 0, 0);
108 
109  // Redefine the disk numbering to use the sign
110  auto pxendcap = extractors[intern("PXEndcap")];
111  auto diskid = intern("PXDisk");
112  auto pxdisk = extractors[diskid];
113  extractors[diskid] = [pxdisk, pxendcap](InterestingQuantities const& iq) {
114  auto disk = pxdisk(iq);
115  if (disk == UNDEFINED)
116  return UNDEFINED;
117  auto endcap = pxendcap(iq);
118  return endcap == 1 ? -disk : disk;
119  };
120 
121  // DetId and module names
122  auto detid = [](InterestingQuantities const& iq) {
123  uint32_t id = iq.sourceModule.rawId();
124  return Value(id);
125  };
126  addExtractor(intern("DetId"), detid, 0, 0 // No sane value possible here.
127  );
128  // these are just aliases with special handling in formatting
129  // the names are created with PixelBarrelName et. al. later
130  addExtractor(intern("PXModuleName"), detid, 0, 0);
131 
132  int phase = iConfig.getParameter<int>("upgradePhase");
133  bool isUpgrade = phase == 1;
134 
135  // Get a Geometry
136  edm::ESHandle<TrackerGeometry> trackerGeometryHandle;
137  iSetup.get<TrackerDigiGeometryRecord>().get(trackerGeometryHandle);
138  assert(trackerGeometryHandle.isValid());
139 
140  // Now traverse the detector and collect whatever we need.
141  auto detids = trackerGeometryHandle->detIds();
142  for (DetId id : detids) {
143  if (id.subdetId() != PixelSubdetector::PixelBarrel && id.subdetId() != PixelSubdetector::PixelEndcap)
144  continue;
145  auto iq = InterestingQuantities{nullptr, id, 0, 0};
146 
147  // prepare pretty names
148  std::string name = "";
149  if (id.subdetId() == PixelSubdetector::PixelBarrel) { // Barrel
150  PixelBarrelName mod(id, tt, isUpgrade);
151  name = mod.name();
152  } else { // assume Endcap
153  PixelEndcapName mod(id, tt, isUpgrade);
154  name = mod.name();
155  }
156  format_value[std::make_pair(intern("PXModuleName"), Value(id.rawId()))] = name;
157 
158  // we record each module 4 times, one for each corner, so we also get ROCs
159  // in booking (at least for the ranges)
160  const PixelGeomDetUnit* detUnit = dynamic_cast<const PixelGeomDetUnit*>(trackerGeometryHandle->idToDetUnit(id));
161  assert(detUnit);
162  const PixelTopology* topo = &detUnit->specificTopology();
163  iq.row = 0;
164  iq.col = 0;
165  all_modules.push_back(iq);
166  iq.row = topo->nrows() - 1;
167  iq.col = 0;
168  all_modules.push_back(iq);
169  iq.row = 0;
170  iq.col = topo->ncolumns() - 1;
171  all_modules.push_back(iq);
172  iq.row = topo->nrows() - 1;
173  iq.col = topo->ncolumns() - 1;
174  all_modules.push_back(iq);
175  }
176 }
177 
179  // TODO: SiPixelCoordinates has a large overlap with theis GeometryInterface
180  // in general.
181  // Rough convention is to use own code for things that are easy and fast to
182  // determine, and use SiPixelCoordinates for complicated things.
183  // SiPixelCoordinates uses lookup maps for everything, so it is faster than
184  // most other code, but still slow on DQM scales.
185  int phase = iConfig.getParameter<int>("upgradePhase");
186 
187  // this shared pointer is kept alive by the references in the lambdas that follow.
188  // That is a bit less obvious than keeping it as a member but more correct.
189  auto coord = std::make_shared<SiPixelCoordinates>(phase);
190 
191  // note that we should reeinit for each event. But this probably won't explode
192  // thanks to the massive memoization in SiPixelCoordinates which is completely
193  // initialized while booking.
194  coord->init(iSetup);
195 
196  // SiPixelCoordinates uses a different convention for UNDEFINED:
197  auto from_coord = [](double in) { return (in == -9999.0) ? UNDEFINED : Value(in); };
198 
199  // Rings are a concept that cannot be derived from bitmasks.
200  addExtractor(intern("PXRing"), [coord, from_coord](InterestingQuantities const& iq) {
201  return from_coord(coord->ring(iq.sourceModule));
202  });
203 
204  // Quadrant names.
205  auto pxbarrel = extractors[intern("PXBarrel")];
206  addExtractor(
207  intern("HalfCylinder"),
208  [coord, pxbarrel](InterestingQuantities const& iq) {
209  if (pxbarrel(iq) != UNDEFINED)
210  return UNDEFINED;
211  int quadrant = coord->quadrant(iq.sourceModule);
212  switch (quadrant) {
213  case 1:
214  return Value(12); // mO
215  case 2:
216  return Value(11); // mI
217  case 3:
218  return Value(22); // pO
219  case 4:
220  return Value(21); // pI
221  default:
222  return UNDEFINED;
223  }
224  },
225  0,
226  0 // N/A
227  );
228  addExtractor(
229  intern("Shell"),
230  [coord, pxbarrel](InterestingQuantities const& iq) {
231  if (pxbarrel(iq) == UNDEFINED)
232  return UNDEFINED;
233  int quadrant = coord->quadrant(iq.sourceModule);
234  switch (quadrant) {
235  case 1:
236  return Value(12); // mO
237  case 2:
238  return Value(11); // mI
239  case 3:
240  return Value(22); // pO
241  case 4:
242  return Value(21); // pI
243  default:
244  return UNDEFINED;
245  }
246  },
247  0,
248  0 // N/A
249  );
250 
251  // Online Numbering.
252  addExtractor(intern("SignedLadder"), [coord, from_coord](InterestingQuantities const& iq) {
253  return from_coord(coord->signed_ladder(iq.sourceModule()));
254  });
255  addExtractor(intern("SignedModule"), [coord, from_coord](InterestingQuantities const& iq) {
256  return from_coord(coord->signed_module(iq.sourceModule()));
257  });
258  addExtractor(intern("SignedBlade"), [coord, from_coord](InterestingQuantities const& iq) {
259  return from_coord(coord->signed_blade(iq.sourceModule()));
260  });
261 
262  // Flipped and Outer ladders
263  addExtractor(intern("OuterLadder"), [coord, from_coord](InterestingQuantities const& iq) {
264  return from_coord(coord->outer(iq.sourceModule()));
265  });
266  addExtractor(intern("FlippedLadder"), [coord, from_coord](InterestingQuantities const& iq) {
267  return from_coord(coord->flipped(iq.sourceModule()));
268  });
269 
270  // Pixel Map axis.
271  // TODO: automatic range and binning for phase0 are incorrect.
272  // Should be set manually here.
273  addExtractor(
274  intern("SignedModuleCoord"), // BPIX x
275  [coord, from_coord](InterestingQuantities const& iq) {
276  return from_coord(coord->signed_module_coord(iq.sourceModule(), std::make_pair(int(iq.row), int(iq.col))));
277  },
278  UNDEFINED,
279  UNDEFINED,
280  1.0 / 8.0);
281  addExtractor(
282  intern("SignedLadderCoord"), // BPIX y
283  [coord, from_coord](InterestingQuantities const& iq) {
284  return from_coord(coord->signed_ladder_coord(iq.sourceModule(), std::make_pair(int(iq.row), int(iq.col))));
285  },
286  UNDEFINED,
287  UNDEFINED,
288  1.0 / 2.0);
289  addExtractor(
290  intern("SignedDiskCoord"), // FPIX x (per-ring)
291  [coord, from_coord](InterestingQuantities const& iq) {
292  return from_coord(coord->signed_disk_coord(iq.sourceModule(), std::make_pair(int(iq.row), int(iq.col))));
293  },
294  UNDEFINED,
295  UNDEFINED,
296  1.0 / 8.0);
297  addExtractor(
298  intern("SignedDiskRingCoord"), // FPIX x (FPIX-as-one-plot)
299  [coord, from_coord](InterestingQuantities const& iq) {
300  return from_coord(coord->signed_disk_ring_coord(iq.sourceModule(), std::make_pair(int(iq.row), int(iq.col))));
301  },
302  UNDEFINED,
303  UNDEFINED,
304  1.0 / 16.0);
305  addExtractor(
306  intern("SignedBladePanelCoord"), // FPIX y
307  [coord, from_coord, phase](InterestingQuantities const& iq) {
308  if (phase == 0) {
309  return from_coord(coord->signed_blade_coord(iq.sourceModule(), std::make_pair(int(iq.row), int(iq.col))));
310  } else if (phase == 1) {
311  return from_coord(
312  coord->signed_blade_panel_coord(iq.sourceModule(), std::make_pair(int(iq.row), int(iq.col))));
313  } else {
314  return UNDEFINED; // TODO: phase2
315  }
316  },
317  UNDEFINED,
318  UNDEFINED,
319  phase == 1 ? 0.25 : 0.2);
320  addExtractor(
321  intern("SignedShiftedBladePanelCoord"), // FPIX-as-one y
322  [coord, from_coord, phase](InterestingQuantities const& iq) {
323  if (phase == 0) {
324  return from_coord(coord->signed_blade_coord(iq.sourceModule(), std::make_pair(int(iq.row), int(iq.col))));
325  } else if (phase == 1) {
326  return from_coord(
327  coord->signed_shifted_blade_panel_coord(iq.sourceModule(), std::make_pair(int(iq.row), int(iq.col))));
328  } else {
329  return UNDEFINED; // TODO: phase2
330  }
331  },
332  UNDEFINED,
333  UNDEFINED,
334  phase == 1 ? 0.25 : 0.1 // half-roc for phase0
335  );
336  addExtractor(
337  intern("SignedBladePanel"), // per-module FPIX y
338  [coord, from_coord](InterestingQuantities const& iq) {
339  return from_coord(coord->signed_blade_panel_coord(iq.sourceModule(), std::make_pair(int(iq.row), int(iq.col))));
340  },
341  UNDEFINED,
342  UNDEFINED,
343  1.0 / 2.0);
344 
345  addExtractor(
346  intern("SignedBladePanel"),
347  [coord, from_coord](InterestingQuantities const& iq) {
348  return from_coord(coord->signed_blade_panel_coord(iq.sourceModule(), std::make_pair(int(iq.row), int(iq.col))));
349  },
350  UNDEFINED,
351  UNDEFINED,
352  1.0 / 2.0);
353 
354  // more readout-related things.
355  addExtractor(intern("ROC"), [coord, from_coord](InterestingQuantities const& iq) {
356  return from_coord(coord->roc(iq.sourceModule(), std::make_pair(int(iq.row), int(iq.col))));
357  });
358  addExtractor(intern("Sector"), [coord, from_coord](InterestingQuantities const& iq) {
359  return from_coord(coord->sector(iq.sourceModule()));
360  });
361  addExtractor(intern("Channel"), [coord, from_coord](InterestingQuantities const& iq) {
362  return from_coord(coord->channel(iq.sourceModule(), std::make_pair(int(iq.row), int(iq.col))));
363  });
364 }
365 
367  // extractors for quantities that are roughly time-based. We cannot book plots based on these; they have to
368  // be grouped away in step1.
369  addExtractor(
370  intern("Lumisection"),
371  [](InterestingQuantities const& iq) {
372  if (!iq.sourceEvent)
373  return UNDEFINED;
374  return Value(iq.sourceEvent->luminosityBlock());
375  },
376  1,
377  iConfig.getParameter<int>("max_lumisection"));
378 
379  int onlineblock = iConfig.getParameter<int>("onlineblock");
380  int n_onlineblocks = iConfig.getParameter<int>("n_onlineblocks");
381  addExtractor(
382  intern("OnlineBlock"),
383  [onlineblock](InterestingQuantities const& iq) {
384  if (!iq.sourceEvent)
385  return UNDEFINED;
386  return Value(onlineblock + iq.sourceEvent->luminosityBlock() / onlineblock);
387  },
388  // note: this range is not visible anywhere (if the RenderPlugin does its job),
389  // but the strange range allows the RenderPlugin to know the block size.
390  onlineblock,
391  onlineblock + n_onlineblocks - 1);
392 
393  int lumiblock = iConfig.getParameter<int>("lumiblock");
394  addExtractor(
395  intern("LumiBlock"),
396  [lumiblock](InterestingQuantities const& iq) {
397  if (!iq.sourceEvent)
398  return UNDEFINED;
399  // The '-1' is for making 1-10 the same block rather than 0-9
400  // The '+0.5' makes the block span an integer range rather n.5-m.5
401  return Value(((iq.sourceEvent->luminosityBlock() - 1) / lumiblock) + 0.5);
402  },
403  -0.5,
404  iConfig.getParameter<int>("max_lumisection") / lumiblock);
405 
406  addExtractor(
407  intern("BX"),
408  [](InterestingQuantities const& iq) {
409  if (!iq.sourceEvent)
410  return UNDEFINED;
411  return Value(iq.sourceEvent->bunchCrossing());
412  },
413  1,
414  iConfig.getParameter<int>("max_bunchcrossing"));
415 }
416 
418  // stuff that is within modules. Might require some phase0/phase1/strip switching later
419  addExtractor(
420  intern("row"),
421  [](InterestingQuantities const& iq) { return Value(iq.row); },
422  0,
423  iConfig.getParameter<int>("module_rows") - 1);
424  addExtractor(
425  intern("col"),
426  [](InterestingQuantities const& iq) { return Value(iq.col); },
427  0,
428  iConfig.getParameter<int>("module_cols") - 1);
429 }
430 
432  auto cablingMapLabel = iConfig.getParameter<std::string>("CablingMapLabel");
434  iSetup.get<SiPixelFedCablingMapRcd>().get(cablingMapLabel, theCablingMap);
435 
436  std::shared_ptr<SiPixelFrameReverter> siPixelFrameReverter =
437  // I think passing the bare pointer here is safe, but who knows...
438  std::make_shared<SiPixelFrameReverter>(iSetup, theCablingMap.operator->());
439 
440  addExtractor(intern("FED"), [siPixelFrameReverter](InterestingQuantities const& iq) {
441  if (iq.sourceModule == 0xFFFFFFFF)
442  return Value(iq.col); // hijacked for the raw data plugin
443  return Value(siPixelFrameReverter->findFedId(iq.sourceModule.rawId()));
444  });
445 
446  // TODO: ranges should be set manually below, since booking probably cannot
447  // infer them correctly (no ROC-level granularity)
448  // PERF: this is slow. Prefer SiPixelCordinates versions here.
449  addExtractor(intern("LinkInFed"), [siPixelFrameReverter](InterestingQuantities const& iq) {
450  if (iq.sourceModule == 0xFFFFFFFF)
451  return Value(iq.row); // hijacked for the raw data plugin
452  sipixelobjects::GlobalPixel gp = {iq.row, iq.col};
453  return Value(siPixelFrameReverter->findLinkInFed(iq.sourceModule.rawId(), gp));
454  });
455  // not sure if this is useful anywhere.
456  addExtractor(intern("RocInLink"), [siPixelFrameReverter](InterestingQuantities const& iq) {
458  return Value(siPixelFrameReverter->findRocInLink(iq.sourceModule.rawId(), gp));
459  });
460  // This might be equivalent to our ROC numbering.
461  addExtractor(intern("RocInDet"), [siPixelFrameReverter](InterestingQuantities const& iq) {
463  return Value(siPixelFrameReverter->findRocInDet(iq.sourceModule.rawId(), gp));
464  });
465 }
466 
468  auto it = format_value.find(std::make_pair(col, val));
469  if (it != format_value.end())
470  return it->second;
471 
472  // non-number output names (_pO etc.) are hardwired here.
473  std::string name = pretty(col);
474  std::string value = "_" + std::to_string(int(val));
475  if (val == 0)
476  value = ""; // hide Barrel_0 etc.
477  if (name == "PXDisk" && val > 0) // +/- sign for disk num
478  value = "_+" + std::to_string(int(val));
479  // pretty (legacy?) names for Shells and HalfCylinders
480  std::map<int, std::string> shellname{{11, "_mI"}, {12, "_mO"}, {21, "_pI"}, {22, "_pO"}};
481  if (name == "HalfCylinder" || name == "Shell")
482  value = shellname[int(val)];
483  if (val == UNDEFINED)
484  value = "_UNDEFINED";
485  return format_value[std::make_pair(col, val)] = name + value;
486 }
std::map< ID, Value > max_value
bool hasField(const DetId &id, DetIdFields idx) const
T getParameter(std::string const &) const
virtual int nrows() const =0
std::map< std::pair< Column, Value >, std::string > format_value
std::map< std::string, ID > ids
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)
int quadrant(const DetId &detid, const TrackerTopology *tTopo_, bool phase_)
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
std::string formatValue(Column, Value)
int bunchCrossing() const
Definition: EventBase.h:64
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:61
std::string name() const override
from base class
global coordinates (row and column in DetUnit, as in PixelDigi)
Definition: GlobalPixel.h:6
const TrackerGeomDet * idToDetUnit(DetId) const override
Return the pointer to the GeomDetUnit corresponding to a given DetId.
void loadFromTopology(edm::EventSetup const &iSetup, const edm::ParameterSet &iConfig)
void load(edm::EventSetup const &iSetup)
const edm::ParameterSet iConfig
const DetIdContainer & detIds() const override
Returm a vector of all GeomDet DetIds (including those of GeomDetUnits)
std::vector< InterestingQuantities > all_modules
std::string name() const override
from base class
Definition: value.py:1
static const Value UNDEFINED
Definition: DetId.h:17
virtual const PixelTopology & specificTopology() const
Returns a reference to the pixel proxy topology.
unsigned int getField(const DetId &id, DetIdFields idx) const
void loadFEDCabling(edm::EventSetup const &iSetup, const edm::ParameterSet &iConfig)
virtual int ncolumns() const =0
T get() const
Definition: EventSetup.h:73
col
Definition: cuy.py:1010
void loadModuleLevel(edm::EventSetup const &iSetup, const edm::ParameterSet &iConfig)
std::vector< std::function< Value(InterestingQuantities const &iq)> > extractors
std::map< ID, Value > min_value
bool isValid() const
Definition: ESHandle.h:44
ID intern(std::string const &id)
T mod(const T &a, const T &b)
Definition: ecalDccMap.h:4
void loadTimebased(edm::EventSetup const &iSetup, const edm::ParameterSet &iConfig)
void loadFromSiPixelCoordinates(edm::EventSetup const &iSetup, const edm::ParameterSet &iConfig)