CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
28 
29 // Pixel names
32 
33 // C++ stuff
34 #include <cassert>
35 #include <cstdint>
36 #include <iostream>
37 #include <iomanip>
38 #include <memory>
39 
41 
43  //loadFromAlignment(iSetup, iConfig);
44  loadFromTopology(iSetup, iConfig);
45  loadTimebased(iSetup, iConfig);
46  loadModuleLevel(iSetup, iConfig);
47  loadFEDCabling(iSetup, iConfig);
49  edm::LogInfo log("GeometryInterface");
50  log << "Known colum names:\n";
51  for (auto e : ids) log << "+++ column: " << e.first
52  << " ok " << bool(extractors[e.second]) << " min " << min_value[e.second] << " max " << max_value[e.second] << "\n";
53  is_loaded = true;
54 }
55 
57  // Get a Topology
58  edm::ESHandle<TrackerTopology> trackerTopologyHandle;
59  iSetup.get<TrackerTopologyRcd>().get(trackerTopologyHandle);
60  assert(trackerTopologyHandle.isValid());
61 
62  std::vector<ID> geomquantities;
63 
64  struct TTField {
65  const TrackerTopology* tt;
67  Value operator()(InterestingQuantities const& iq) {
68  if (tt->hasField(iq.sourceModule, field))
69  return tt->getField(iq.sourceModule, field);
70  else
71  return UNDEFINED;
72  };
73  };
74 
75  const TrackerTopology* tt = trackerTopologyHandle.operator->();
76 
77 
78  std::vector<std::pair<std::string, TTField>> namedPartitions {
79  {"PXEndcap" , {tt, TrackerTopology::PFSide}},
80 
81  {"PXLayer" , {tt, TrackerTopology::PBLayer}},
82  {"PXLadder" , {tt, TrackerTopology::PBLadder}},
83  {"PXBModule", {tt, TrackerTopology::PBModule}},
84 
85  {"PXBlade" , {tt, TrackerTopology::PFBlade}},
86  {"PXDisk" , {tt, TrackerTopology::PFDisk}},
87  {"PXPanel" , {tt, TrackerTopology::PFPanel}},
88  {"PXFModule", {tt, TrackerTopology::PFModule}},
89  };
90 
91  for (auto& e : namedPartitions) {
92  geomquantities.push_back(intern(e.first));
93  addExtractor(intern(e.first), e.second, UNDEFINED, UNDEFINED);
94  }
95 
96  auto pxbarrel = [] (InterestingQuantities const& iq) { return iq.sourceModule.subdetId() == PixelSubdetector::PixelBarrel ? 0 : UNDEFINED; };
97  auto pxforward = [] (InterestingQuantities const& iq) { return iq.sourceModule.subdetId() == PixelSubdetector::PixelEndcap ? 0 : UNDEFINED; };
98  addExtractor(intern("PXBarrel"), pxbarrel, 0, 0);
99  addExtractor(intern("PXForward"), pxforward, 0, 0);
100 
101  // Redefine the disk numbering to use the sign
102  auto pxendcap = extractors[intern("PXEndcap")];
103  auto diskid = intern("PXDisk");
104  auto pxdisk = extractors[diskid];
105  extractors[diskid] = [pxdisk, pxendcap] (InterestingQuantities const& iq) {
106  auto disk = pxdisk(iq);
107  if (disk == UNDEFINED) return UNDEFINED;
108  auto endcap = pxendcap(iq);
109  return endcap == 1 ? -disk : disk;
110  };
111 
112  // DetId and module names
113  auto detid = [] (InterestingQuantities const& iq) {
114  uint32_t id = iq.sourceModule.rawId();
115  return Value(id);
116  };
117  addExtractor(intern("DetId"), detid,
118  0, 0 // No sane value possible here.
119  );
120  // these are just aliases with special handling in formatting
121  // the names are created with PixelBarrelName et. al. later
122  addExtractor(intern("PXModuleName"), detid, 0, 0);
123 
124  int phase = iConfig.getParameter<int>("upgradePhase");
125  bool isUpgrade = phase == 1;
126 
127  // Get a Geometry
128  edm::ESHandle<TrackerGeometry> trackerGeometryHandle;
129  iSetup.get<TrackerDigiGeometryRecord>().get(trackerGeometryHandle);
130  assert(trackerGeometryHandle.isValid());
131 
132  // some parameters to record the ROCs here
133  auto module_rows = iConfig.getParameter<int>("module_rows") - 1;
134  auto module_cols = iConfig.getParameter<int>("module_cols") - 1;
135 
136  // Now traverse the detector and collect whatever we need.
137  auto detids = trackerGeometryHandle->detIds();
138  for (DetId id : detids) {
139  if (id.subdetId() != PixelSubdetector::PixelBarrel && id.subdetId() != PixelSubdetector::PixelEndcap) continue;
140  auto iq = InterestingQuantities{nullptr, id, 0, 0};
141 
142  // prepare pretty names
143  std::string name = "";
144  if (id.subdetId() == PixelSubdetector::PixelBarrel) { // Barrel
145  PixelBarrelName mod(id, tt, isUpgrade);
146  name = mod.name();
147  } else { // assume Endcap
148  PixelEndcapName mod(id, tt, isUpgrade);
149  name = mod.name();
150  }
151  format_value[std::make_pair(intern("PXModuleName"), Value(id.rawId()))] = name;
152 
153  // we record each module 4 times, one for each corner, so we also get ROCs
154  // in booking (at least for the ranges)
155  // TODO: add all ROCs?
156  // TODO: Things are more complicated for phase2, and we support that via
157  // SiPixelCoordinates, so we should support it here too.
158  iq.row = 1; iq.col = 1;
159  all_modules.push_back(iq);
160  iq.row = module_rows-1; iq.col = 1;
161  all_modules.push_back(iq);
162  iq.row = 1; iq.col = module_cols-1;
163  all_modules.push_back(iq);
164  iq.row = module_rows-1; iq.col = module_cols-1;
165  all_modules.push_back(iq);
166  }
167 }
168 
170  // TODO: SiPixelCoordinates has a large overlap with theis GeometryInterface
171  // in general.
172  // Rough convention is to use own code for things that are easy and fast to
173  // determine, and use SiPixelCoordinates for complicated things.
174  // SiPixelCoordinates uses lookup maps for everything, so it is faster than
175  // most other code, but still slow on DQM scales.
176  int phase = iConfig.getParameter<int>("upgradePhase");
177 
178  // this shared pointer is kept alive by the references in the lambdas that follow.
179  // That is a bit less obvious than keeping it as a member but more correct.
180  auto coord = std::make_shared<SiPixelCoordinates>(phase);
181 
182  // note that we should reeinit for each event. But this probably won't explode
183  // thanks to the massive memoization in SiPixelCoordinates which is completely
184  // initialized while booking.
185  coord->init(iSetup);
186 
187  // SiPixelCoordinates uses a different convention for UNDEFINED:
188  auto from_coord = [](double in) { return (in == -9999.0) ? UNDEFINED : Value(in); };
189 
190  // Rings are a concept that cannot be derived from bitmasks.
191  addExtractor(intern("PXRing"),
192  [coord, from_coord] (InterestingQuantities const& iq) {
193  return from_coord(coord->ring(iq.sourceModule));
194  }
195  );
196 
197  // Quadrant names.
198  auto pxbarrel = extractors[intern("PXBarrel")];
199  addExtractor(intern("HalfCylinder"),
200  [coord, pxbarrel] (InterestingQuantities const& iq) {
201  if (pxbarrel(iq) != UNDEFINED) return UNDEFINED;
202  int quadrant = coord->quadrant(iq.sourceModule);
203  switch (quadrant) {
204  case 1: return Value(12); // mO
205  case 2: return Value(11); // mI
206  case 3: return Value(22); // pO
207  case 4: return Value(21); // pI
208  default: return UNDEFINED;
209  }
210  }, 0, 0 // N/A
211  );
212  addExtractor(intern("Shell"),
213  [coord, pxbarrel] (InterestingQuantities const& iq) {
214  if (pxbarrel(iq) == UNDEFINED) return UNDEFINED;
215  int quadrant = coord->quadrant(iq.sourceModule);
216  switch (quadrant) {
217  case 1: return Value(12); // mO
218  case 2: return Value(11); // mI
219  case 3: return Value(22); // pO
220  case 4: return Value(21); // pI
221  default: return UNDEFINED;
222  }
223  }, 0, 0 // N/A
224  );
225 
226  // Online Numbering.
227  addExtractor(intern("SignedLadder"),
228  [coord, from_coord] (InterestingQuantities const& iq) {
229  return from_coord(coord->signed_ladder(iq.sourceModule()));
230  }
231  );
232  addExtractor(intern("SignedModule"),
233  [coord, from_coord] (InterestingQuantities const& iq) {
234  return from_coord(coord->signed_module(iq.sourceModule()));
235  }
236  );
237  addExtractor(intern("SignedBlade"),
238  [coord, from_coord] (InterestingQuantities const& iq) {
239  return from_coord(coord->signed_blade(iq.sourceModule()));
240  }
241  );
242 
243  // Pixel Map axis.
244  // TODO: binning should be phase-dependent. Or maybe even per-plot configurable.
245  addExtractor(intern("SignedModuleCoord"),
246  [coord, from_coord] (InterestingQuantities const& iq) {
247  return from_coord(coord->signed_module_coord(iq.sourceModule(),
248  std::make_pair(int(iq.row), int(iq.col))));
249  }, UNDEFINED, UNDEFINED, 1.0/8.0
250  );
251  addExtractor(intern("SignedLadderCoord"),
252  [coord, from_coord] (InterestingQuantities const& iq) {
253  return from_coord(coord->signed_ladder_coord(iq.sourceModule(),
254  std::make_pair(int(iq.row), int(iq.col))));
255  }, UNDEFINED, UNDEFINED, 1.0/2.0
256  );
257  addExtractor(intern("SignedDiskCoord"),
258  [coord, from_coord] (InterestingQuantities const& iq) {
259  return from_coord(coord->signed_disk_coord(iq.sourceModule(),
260  std::make_pair(int(iq.row), int(iq.col))));
261  }, UNDEFINED, UNDEFINED, 1.0/8.0
262  );
263  addExtractor(intern("SignedBladePanelCoord"),
264  [coord, from_coord, phase] (InterestingQuantities const& iq) {
265  if (phase == 0) {
266  return from_coord(coord->signed_blade_coord(
267  iq.sourceModule(), std::make_pair(int(iq.row), int(iq.col))));
268  } else if (phase == 1) {
269  return from_coord(coord->signed_blade_panel_coord(
270  iq.sourceModule(), std::make_pair(int(iq.row), int(iq.col))));
271  } else {
272  // TODO: phase2
273  return UNDEFINED;
274  }
275  }, UNDEFINED, UNDEFINED, phase == 1 ? 0.25 : 0.2
276  );
277 
278  addExtractor(intern("SignedBladePanel"),
279  [coord, from_coord] (InterestingQuantities const& iq) {
280  return from_coord(coord->signed_blade_panel_coord(iq.sourceModule(), std::make_pair(int(iq.row), int(iq.col))));
281  }, UNDEFINED, UNDEFINED, 1.0/2.0
282  );
283 
284  // more readout-related things.
285  addExtractor(intern("ROC"),
286  [coord, from_coord] (InterestingQuantities const& iq) {
287  return from_coord(coord->roc(iq.sourceModule(),
288  std::make_pair(int(iq.row), int(iq.col))));
289  }
290  );
291  addExtractor(intern("Sector"),
292  [coord, from_coord] (InterestingQuantities const& iq) {
293  return from_coord(coord->sector(iq.sourceModule()));
294  }
295  );
296 
297 }
298 
300  // extractors for quantities that are roughly time-based. We cannot book plots based on these; they have to
301  // be grouped away in step1.
302  addExtractor(intern("Lumisection"),
303  [] (InterestingQuantities const& iq) {
304  if(!iq.sourceEvent) return UNDEFINED;
305  return Value(iq.sourceEvent->luminosityBlock());
306  },
307  1, iConfig.getParameter<int>("max_lumisection")
308  );
309  int onlineblock = iConfig.getParameter<int>("onlineblock");
310  int n_onlineblocks = iConfig.getParameter<int>("n_onlineblocks");
311  addExtractor(intern("OnlineBlock"),
312  [onlineblock] (InterestingQuantities const& iq) {
313  if(!iq.sourceEvent) return UNDEFINED;
314  return Value(onlineblock + iq.sourceEvent->luminosityBlock() / onlineblock);
315  },
316  // note: this range is not visible anywhere (if the RenderPlugin does its job),
317  // but the strange range allows the RenderPlugin to know the block size.
318  onlineblock, onlineblock+n_onlineblocks-1
319  );
320  addExtractor(intern("BX"),
321  [] (InterestingQuantities const& iq) {
322  if(!iq.sourceEvent) return UNDEFINED;
323  return Value(iq.sourceEvent->bunchCrossing());
324  },
325  1, iConfig.getParameter<int>("max_bunchcrossing")
326  );
327 }
328 
330  // stuff that is within modules. Might require some phase0/phase1/strip switching later
331  addExtractor(intern("row"),
332  [] (InterestingQuantities const& iq) {
333  return Value(iq.row);
334  },
335  0, iConfig.getParameter<int>("module_rows") - 1
336  );
337  addExtractor(intern("col"),
338  [] (InterestingQuantities const& iq) {
339  return Value(iq.col);
340  },
341  0, iConfig.getParameter<int>("module_cols") - 1
342  );
343 
344 }
345 
347  auto cablingMapLabel = iConfig.getParameter<std::string>("CablingMapLabel");
349  iSetup.get<SiPixelFedCablingMapRcd>().get(cablingMapLabel, theCablingMap);
350 
351  std::shared_ptr<SiPixelFrameReverter> siPixelFrameReverter =
352  // I think passing the bare pointer here is safe, but who knows...
353  std::make_shared<SiPixelFrameReverter>(iSetup, theCablingMap.operator->());
354 
355  addExtractor(intern("FED"),
356  [siPixelFrameReverter] (InterestingQuantities const& iq) {
357  if (iq.sourceModule == 0xFFFFFFFF)
358  return Value(iq.col); // hijacked for the raw data plugin
359  return Value(siPixelFrameReverter->findFedId(iq.sourceModule.rawId()));
360  }
361  );
362 
363  // TODO: ranges should be set manually below, since booking probably cannot
364  // infer them correctly (no ROC-level granularity)
365  addExtractor(intern("LinkInFed"),
366  [siPixelFrameReverter] (InterestingQuantities const& iq) {
367  if (iq.sourceModule == 0xFFFFFFFF)
368  return Value(iq.row); // hijacked for the raw data plugin
370  return Value(siPixelFrameReverter->findLinkInFed(iq.sourceModule.rawId(), gp));
371  }
372  );
373  // not sure if this is useful anywhere.
374  addExtractor(intern("RocInLink"),
375  [siPixelFrameReverter] (InterestingQuantities const& iq) {
376  sipixelobjects::GlobalPixel gp = {iq.row, iq.col};
377  return Value(siPixelFrameReverter->findRocInLink(iq.sourceModule.rawId(), gp));
378  }
379  );
380  // This might be equivalent to our ROC numbering.
381  addExtractor(intern("RocInDet"),
382  [siPixelFrameReverter] (InterestingQuantities const& iq) {
383  sipixelobjects::GlobalPixel gp = {iq.row, iq.col};
384  return Value(siPixelFrameReverter->findRocInDet(iq.sourceModule.rawId(), gp));
385  }
386  );
387 }
388 
390  auto it = format_value.find(std::make_pair(col, val));
391  if (it != format_value.end()) return it->second;
392 
393  // non-number output names (_pO etc.) are hardwired here.
394  std::string name = pretty(col);
395  std::string value = "_" + std::to_string(int(val));
396  if (val == 0) value = ""; // hide Barrel_0 etc.
397  if (name == "PXDisk" && val > 0) // +/- sign for disk num
398  value = "_+" + std::to_string(int(val));
399  // pretty (legacy?) names for Shells and HalfCylinders
400  std::map<int, std::string> shellname{
401  {11, "_mI"}, {12, "_mO"}, {21, "_pI"}, {22, "_pO"}};
402  if (name == "HalfCylinder" || name == "Shell") value = shellname[int(val)];
403  if (val == UNDEFINED) value = "_UNDEFINED";
404  return format_value[std::make_pair(col, val)] = name+value;
405 }
406 
std::map< ID, Value > max_value
T getParameter(std::string const &) const
std::map< std::pair< Column, Value >, std::string > format_value
std::map< std::string, ID > ids
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)
int bunchCrossing() const
Definition: EventBase.h:64
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:61
global coordinates (row and column in DetUnit, as in PixelDigi)
Definition: GlobalPixel.h:6
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
void loadFromTopology(edm::EventSetup const &iSetup, const edm::ParameterSet &iConfig)
void load(edm::EventSetup const &iSetup)
const edm::ParameterSet iConfig
std::vector< InterestingQuantities > all_modules
reco::JetExtendedAssociation::JetExtendedData Value
static const Value UNDEFINED
Definition: DetId.h:18
virtual std::string name() const
from base class
const T & get() const
Definition: EventSetup.h:56
void loadFEDCabling(edm::EventSetup const &iSetup, const edm::ParameterSet &iConfig)
virtual std::string name() const
from base class
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:47
int col
Definition: cuy.py:1008
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)