CMS 3D CMS Logo

MomentumDependentPedeLabeler.cc
Go to the documentation of this file.
1 
11 #include <algorithm>
12 
15 
23 
25 
26 //___________________________________________________________________________
29  : PedeLabelerBase(alignables, config),
30  theOpenMomentumRange(std::pair<float,float>(0.0, 10000.0)),
31  theMaxNumberOfParameterInstances(0)
32 {
33  align::Alignables alis;
34  alis.push_back(alignables.aliTracker_);
35  alis.push_back(alignables.aliMuon_);
36 
37  if (alignables.aliExtras_) {
38  for (const auto& ali: alignables.aliExtras_->components()) {
39  alis.push_back(ali);
40  }
41  }
42 
43  this->buildMomentumDependencyMap(alignables.aliTracker_,
44  alignables.aliMuon_,
45  alignables.aliExtras_,
46  config);
47  this->buildMap(alis);
48  this->buildReverseMap(); // needed already now to 'fill' theMaxNumberOfParameterInstances
49 }
50 
51 //___________________________________________________________________________
52 
54 {
55 }
56 
57 //___________________________________________________________________________
60 {
61  if (!alignable) return 0;
62 
63  AlignableToIdMap::const_iterator position = theAlignableToIdMap.find(alignable);
64  if (position != theAlignableToIdMap.end()) {
65  return position->second;
66  } else {
67  const DetId detId(alignable->id());
68  //throw cms::Exception("LogicError")
69  edm::LogError("LogicError")
70  << "@SUB=MomentumDependentPedeLabeler::alignableLabel" << "Alignable "
71  << typeid(*alignable).name() << " not in map, det/subdet/alignableStructureType = "
72  << detId.det() << "/" << detId.subdetId() << "/" << alignable->alignableObjectId();
73  return 0;
74  }
75 }
76 
77 //___________________________________________________________________________
78 // Return 32-bit unique label for alignable, 0 indicates failure.
80  unsigned int param,
81  unsigned int instance) const
82 {
83  if (!alignable) return 0;
84 
85  AlignableToIdMap::const_iterator position = theAlignableToIdMap.find(alignable);
86  if (position != theAlignableToIdMap.end()) {
87  AlignableToMomentumRangeMap::const_iterator positionAli = theAlignableToMomentumRangeMap.find(alignable);
88  if (positionAli != theAlignableToMomentumRangeMap.end()) {
89  MomentumRangeParamMap::const_iterator positionParam = (*positionAli).second.find(param);
90  if (positionParam!=(*positionAli).second.end()) {
91  if (instance>=(*positionParam).second.size()) {
92  throw cms::Exception("Alignment") << "@SUB=MomentumDependentPedeLabeler::alignableLabelFromParamAndMomentum"
93  << "iovIdx out of bounds";
94  }
95  return position->second + instance * theParamInstanceOffset;
96  } else {
97  return position->second;
98  }
99  } else {
100  return position->second;
101  }
102  } else {
103  const DetId detId(alignable->id());
104  //throw cms::Exception("LogicError")
105  edm::LogError("LogicError")
106  << "@SUB=MomentumDependentPedeLabeler::alignableLabel" << "Alignable "
107  << typeid(*alignable).name() << " not in map, det/subdet/alignableStructureType = "
108  << detId.det() << "/" << detId.subdetId() << "/" << alignable->alignableObjectId();
109  return 0;
110  }
111 }
112 
113 //_________________________________________________________________________
114 unsigned int MomentumDependentPedeLabeler::lasBeamLabel(unsigned int lasBeamId) const
115 {
116  UintUintMap::const_iterator position = theLasBeamToLabelMap.find(lasBeamId);
117  if (position != theLasBeamToLabelMap.end()) {
118  return position->second;
119  } else {
120  //throw cms::Exception("LogicError")
121  edm::LogError("LogicError") << "@SUB=MomentumDependentPedeLabeler::lasBeamLabel"
122  << "No label for beam Id " << lasBeamId;
123  return 0;
124  }
125 }
126 
127 //_________________________________________________________________________
128 unsigned int MomentumDependentPedeLabeler::parameterLabel(unsigned int aliLabel, unsigned int parNum) const
129 {
130  if (parNum >= theMaxNumParam) {
131  throw cms::Exception("Alignment") << "@SUB=MomentumDependentPedeLabeler::parameterLabel"
132  << "Parameter number " << parNum
133  << " out of range 0 <= num < " << theMaxNumParam;
134  }
135  return aliLabel + parNum;
136 }
137 
138 //_________________________________________________________________________
139 unsigned int MomentumDependentPedeLabeler::parameterLabel(Alignable *alignable, unsigned int parNum,
141  const TrajectoryStateOnSurface &tsos) const
142 {
143  if (!alignable) return 0;
144 
145  if (parNum >= theMaxNumParam) {
146  throw cms::Exception("Alignment") << "@SUB=MomentumDependentPedeLabeler::parameterLabel"
147  << "Parameter number " << parNum
148  << " out of range 0 <= num < " << theMaxNumParam;
149  }
150 
151  AlignableToIdMap::const_iterator position = theAlignableToIdMap.find(alignable);
152  if (position != theAlignableToIdMap.end()) {
153 
154  AlignableToMomentumRangeMap::const_iterator positionAli = theAlignableToMomentumRangeMap.find(alignable);
155  if (positionAli != theAlignableToMomentumRangeMap.end()) {
156 
157  MomentumRangeParamMap::const_iterator positionParam = (*positionAli).second.find(parNum);
158  if (positionParam!=(*positionAli).second.end()) {
159 
160  int offset = 0;
161  float mom = tsos.globalMomentum().mag();
162  const MomentumRangeVector & momentumRanges = (*positionParam).second;
163  for (const auto& iMomentum: momentumRanges) {
164 
165  if (iMomentum.first<=mom && mom<iMomentum.second) {
166  return position->second + offset * theParamInstanceOffset + parNum;
167  }
168  offset++;
169  }
170  const DetId detId(alignable->id());
171  edm::LogError("LogicError")
172  << "@SUB=MomentumDependentPedeLabeler::alignableLabel" << "Alignable "
173  << typeid(*alignable).name() << " not in map, det/subdet/alignableStructureType = "
174  << detId.det() << "/" << detId.subdetId() << "/" << alignable->alignableObjectId();
175  return 0;
176  } else {
177  return position->second + parNum;
178  }
179 
180  } else {
181  return position->second + parNum;
182  }
183  } else {
184  const DetId detId(alignable->id());
185  //throw cms::Exception("LogicError")
186  edm::LogError("LogicError")
187  << "@SUB=MomentumDependentPedeLabeler::alignableLabel" << "Alignable "
188  << typeid(*alignable).name() << " not in map, det/subdet/alignableStructureType = "
189  << detId.det() << "/" << detId.subdetId() << "/" << alignable->alignableObjectId();
190  return 0;
191  }
192 }
193 
194 //_________________________________________________________________________
196 {
197  AlignableToMomentumRangeMap::const_iterator positionAli = theAlignableToMomentumRangeMap.find(alignable);
198  if (positionAli != theAlignableToMomentumRangeMap.end()) return true;
199  return false;
200 }
201 
202 //_________________________________________________________________________
204 {
205  AlignableToMomentumRangeMap::const_iterator positionAli = theAlignableToMomentumRangeMap.find(alignable);
206  if (positionAli != theAlignableToMomentumRangeMap.end()) {
207 
208  size_t nMomentums = 1;
209  if (param==-1) {
210  for (const auto& iParam: (*positionAli).second) {
211  nMomentums = std::max(nMomentums, iParam.second.size());
212  }
213  return nMomentums;
214  } else {
215  MomentumRangeParamMap::const_iterator iParam = (*positionAli).second.find(param);
216  if (iParam != (*positionAli).second.end()) {
217  return iParam->second.size();
218  } else {
219  return 1;
220  }
221  }
222  }
223 
224  return 1;
225 }
226 
227 //___________________________________________________________________________
228 unsigned int MomentumDependentPedeLabeler::paramNumFromLabel(unsigned int paramLabel) const
229 {
230  if (paramLabel < theMinLabel) {
231  edm::LogError("LogicError") << "@SUB=MomentumDependentPedeLabeler::paramNumFromLabel"
232  << "Label " << paramLabel << " should be >= " << theMinLabel;
233  return 0;
234  }
235  return (paramLabel - theMinLabel) % theParamInstanceOffset;
236 }
237 
238 //___________________________________________________________________________
239 unsigned int MomentumDependentPedeLabeler::alignableLabelFromLabel(unsigned int paramLabel) const
240 {
241  return paramLabel - this->paramNumFromLabel(paramLabel);
242 }
243 
244 //___________________________________________________________________________
246 {
247  const unsigned int aliLabel = this->alignableLabelFromLabel(label);
248  if (aliLabel < theMinLabel) return nullptr; // error already given
249 
250  IdToAlignableMap::const_iterator position = theIdToAlignableMap.find(aliLabel);
251  if (position != theIdToAlignableMap.end()) {
252  return position->second;
253  } else {
254  // error only if not in lasBeamMap:
255  UintUintMap::const_iterator position = theLabelToLasBeamMap.find(aliLabel);
256  if (position == theLabelToLasBeamMap.end()) {
257  edm::LogError("LogicError") << "@SUB=MomentumDependentPedeLabeler::alignableFromLabel"
258  << "Alignable label " << aliLabel << " not in map.";
259  }
260  return nullptr;
261  }
262 }
263 
264 //___________________________________________________________________________
266 {
267  const unsigned int aliLabel = this->alignableLabelFromLabel(label);
268  if (aliLabel < theMinLabel) return 0; // error already given
269 
270  UintUintMap::const_iterator position = theLabelToLasBeamMap.find(aliLabel);
271  if (position != theLabelToLasBeamMap.end()) {
272  return position->second;
273  } else {
274  edm::LogError("LogicError") << "@SUB=MomentumDependentPedeLabeler::lasBeamIdFromLabel"
275  << "Alignable label " << aliLabel << " not in map.";
276  return 0;
277  }
278 }
279 
280 //__________________________________________________________________________________________________
281 std::vector<std::string> MomentumDependentPedeLabeler::decompose(const std::string &s,
283 {
284  std::vector<std::string> result;
285 
286  std::string::size_type previousPos = 0;
287  while (true) {
288  const std::string::size_type delimiterPos = s.find(delimiter, previousPos);
289  if (delimiterPos == std::string::npos) {
290  result.push_back(s.substr(previousPos)); // until end
291  break;
292  }
293  result.push_back(s.substr(previousPos, delimiterPos - previousPos));
294  previousPos = delimiterPos + 1; // +1: skip delimiter
295  }
296 
297  return result;
298 }
299 
300 //__________________________________________________________________________________________________
301 std::vector<unsigned int> MomentumDependentPedeLabeler::convertParamSel(const std::string &selString) const
302 {
303  std::vector<unsigned int> result;
304  for (std::string::size_type pos = 0; pos < selString.size(); ++pos) {
305  if (selString[pos]=='1') result.push_back(pos);
306  }
307  return result;
308 }
309 
311  AlignableMuon* aliMuon,
312  AlignableExtras *aliExtras,
313  const edm::ParameterSet &config)
314 {
316 
317  AlignmentParameterSelector selector(aliTracker, aliMuon, aliExtras);
318 
319  std::vector<char> paramSelDumthe(6, '1');
320 
321  const auto parameterInstancesVPSet =
322  config.getParameter<std::vector<edm::ParameterSet> >("parameterInstances");
323 
324  for (const auto& iter: parameterInstancesVPSet) {
325 
326  const auto tempMomentumRanges = iter.getParameter<std::vector<std::string> >("momentumRanges");
327  if (tempMomentumRanges.empty()) {
328  throw cms::Exception("BadConfig") << "@SUB=MomentumDependentPedeLabeler::buildMomentumDependencyMap\n"
329  << "MomentumRanges empty\n";
330  }
331 
332  MomentumRangeVector MomentumRanges;
333  float lower;
334  float upper;
335  for (unsigned int iMomentum=0;iMomentum<tempMomentumRanges.size();++iMomentum) {
336  std::vector<std::string> tokens = edm::tokenize(tempMomentumRanges[iMomentum], ":");
337 
338  lower = strtod(tokens[0].c_str(), nullptr);
339  upper = strtod(tokens[1].c_str(), nullptr);
340 
341  MomentumRanges.push_back(std::pair<float,float>(lower, upper));
342  }
343 
344  const auto selStrings = iter.getParameter<std::vector<std::string> >("selector");
345  for (const auto& iSel: selStrings) {
346  std::vector<std::string> decompSel(this->decompose(iSel, ','));
347 
348  if (decompSel.size()!=2) {
349  throw cms::Exception("BadConfig") << "@SUB=MomentumDependentPedeLabeler::buildMomentumDependencyMap\n"
350  << iSel <<" should have at least 2 ','-separated parts\n";
351  }
352 
353  std::vector<unsigned int> selParam = this->convertParamSel(decompSel[1]);
354  selector.clear();
355  selector.addSelection(decompSel[0], paramSelDumthe);
356 
357  const auto& alis = selector.selectedAlignables();
358  for (const auto& iAli: alis) {
359 
360  if(iAli->alignmentParameters() == nullptr) {
361  throw cms::Exception("BadConfig")
362  << "@SUB=MomentumDependentPedeLabeler::buildMomentumDependencyMap\n"
363  << "Momentum dependence configured for alignable of type "
364  << objectIdProvider().idToString(iAli->alignableObjectId())
365  << " at (" << iAli->globalPosition().x() << ","
366  << iAli->globalPosition().y() << ","
367  << iAli->globalPosition().z()<< "), "
368  << "but that has no parameters. Please check that all run "
369  << "momentum parameters are also selected for alignment.\n";
370  }
371 
372  for (const auto& iParam: selParam) {
373  AlignableToMomentumRangeMap::const_iterator positionAli = theAlignableToMomentumRangeMap.find(iAli);
374  if (positionAli!=theAlignableToMomentumRangeMap.end()) {
375 
376  AlignmentParameters *AliParams = (*positionAli).first->alignmentParameters();
377  if (static_cast<int>(selParam[selParam.size()-1]) >= AliParams->size()) {
378  throw cms::Exception("BadConfig") << "@SUB=MomentumDependentPedeLabeler::buildMomentumDependencyMap\n"
379  << "mismatch in number of parameters\n";
380  }
381 
382  MomentumRangeParamMap::const_iterator positionParam = (*positionAli).second.find(iParam);
383  if (positionParam!=(*positionAli).second.end()) {
384  throw cms::Exception("BadConfig") << "@SUB=MomentumDependentPedeLabeler::buildMomentumDependencyMap\n"
385  << "Momentum range for parameter specified twice\n";
386  }
387  }
388 
389  theAlignableToMomentumRangeMap[iAli][iParam] = MomentumRanges;
390  }
391  }
392  }
393  }
394 
395  return theAlignableToMomentumRangeMap.size();
396 }
397 
398 //_________________________________________________________________________
400 {
401  theAlignableToIdMap.clear(); // just in case of re-use...
402 
403  align::Alignables allComps;
404 
405  for (const auto& iAli: alis) {
406  if (iAli) {
407  allComps.push_back(iAli);
408  iAli->recursiveComponents(allComps);
409  }
410  }
411 
412  unsigned int id = theMinLabel;
413  for (const auto& iter: allComps) {
414  theAlignableToIdMap.insert(AlignableToIdPair(iter, id));
415  id += theMaxNumParam;
416  }
417 
418  // also care about las beams
419  theLasBeamToLabelMap.clear(); // just in case of re-use...
420  // FIXME: Temporarily hard code values stolen from
421  // https://twiki.cern.ch/twiki/bin/view/CMS/TkLasTrackBasedInterface#Beam_identifier .
422  unsigned int beamIds[] = { 0, 10, 20, 30, 40, 50, 60, 70, // TEC+ R4
423  1, 11, 21, 31, 41, 51, 61, 71, // TEC+ R6
424  100, 110, 120, 130, 140, 150, 160, 170, // TEC- R4
425  101, 111, 121, 131, 141, 151, 161, 171, // TEC- R6
426  200, 210, 220, 230, 240, 250, 260, 270};// AT
427 
428  const size_t nBeams = sizeof(beamIds)/sizeof(beamIds[0]);
429  for (size_t iBeam = 0; iBeam < nBeams; ++iBeam) {
430  //edm::LogInfo("Alignment") << "Las beam " << beamIds[iBeam] << " gets label " << id << ".";
431  theLasBeamToLabelMap[beamIds[iBeam]] = id;
432  id += theMaxNumParam;
433  }
434 
435  if (id > theParamInstanceOffset) { // 'overflow' per instance
436  throw cms::Exception("Alignment") << "@SUB=MomentumDependentPedeLabeler::buildMap: "
437  << "Too many labels per instance (" << id-1 << ") leading to double use, "
438  << "increase PedeLabelerBase::theParamInstanceOffset!\n";
439  }
440  // return combined size
441  return theAlignableToIdMap.size() + theLasBeamToLabelMap.size();
442 }
443 
444 //_________________________________________________________________________
446 {
447  // alignables
448  theIdToAlignableMap.clear(); // just in case of re-use...
449 
450  for (const auto& it: theAlignableToIdMap) {
451  const unsigned int key = it.second;
452  Alignable *ali = it.first;
453  const unsigned int nInstances = this->numberOfParameterInstances(ali, -1);
455  for (unsigned int iInstance=0;iInstance<nInstances;++iInstance) {
456  theIdToAlignableMap[key+iInstance*theParamInstanceOffset] = ali;
457  }
458  }
459 
460  // las beams
461  theLabelToLasBeamMap.clear(); // just in case of re-use...
462 
463  for (const auto& it: theLasBeamToLabelMap) {
464  theLabelToLasBeamMap[it.second] = it.first; //revert key/value
465  }
466 
467  // return combined size
468  return theIdToAlignableMap.size() + theLabelToLasBeamMap.size();
469 }
470 
unsigned int parameterLabel(unsigned int aliLabel, unsigned int parNum) const override
returns the label for a given alignable parameter number combination
unsigned int alignableLabelFromParamAndInstance(Alignable *alignable, unsigned int param, unsigned int instance) const override
T getParameter(std::string const &) const
align::ID id() const
Return the ID of Alignable, i.e. DetId of &#39;first&#39; component GeomDet(Unit).
Definition: Alignable.h:189
static PFTauRenderPlugin instance
std::vector< std::string > decompose(const std::string &s, std::string::value_type delimiter) const
UintUintMap theLabelToLasBeamMap
labels for las beams
unsigned int paramNumFromLabel(unsigned int paramLabel) const override
parameter number, 0 <= .. < theMaxNumParam, belonging to unique parameter label
static const unsigned int theMinLabel
Definition: config.py:1
uint16_t size_type
MomentumDependentPedeLabeler(const PedeLabelerBase::TopLevelAlignables &alignables, const edm::ParameterSet &config)
constructor from three Alignables (null pointers allowed )
define event information passed to algorithms
static const unsigned int theMaxNumParam
char const * label
void clear()
remove all selected Alignables and geometrical restrictions
T mag() const
Definition: PV3DBase.h:67
unsigned int lasBeamLabel(unsigned int lasBeamId) const override
virtual StructureType alignableObjectId() const =0
Return the alignable type identifier.
unsigned int buildMap(const align::Alignables &)
returns size of map
unsigned int alignableLabel(Alignable *alignable) const override
Return 32-bit unique label for alignable, 0 indicates failure.
unsigned int lasBeamIdFromLabel(unsigned int label) const override
const Alignables & components() const
UintUintMap theLasBeamToLabelMap
reverse map
unsigned int buildReverseMap()
returns size of map
bool hasSplitParameters(Alignable *alignable) const override
returns true if the alignable has parameters that are split into various bins
const align::Alignables & selectedAlignables() const
vector of alignables selected so far
Definition: DetId.h:18
int size(void) const
Get number of parameters.
const char * idToString(align::StructureType type) const
Alignable * alignableFromLabel(unsigned int label) const override
static const unsigned int theParamInstanceOffset
std::vector< Alignable * > Alignables
Definition: Utilities.h:32
std::vector< MomentumRange > MomentumRangeVector
unsigned int addSelection(const std::string &name, const std::vector< char > &paramSel)
unsigned int numberOfParameterInstances(Alignable *alignable, int param=-1) const override
returns the number of instances for a given parameter
unsigned int buildMomentumDependencyMap(AlignableTracker *aliTracker, AlignableMuon *aliMuon, AlignableExtras *extras, const edm::ParameterSet &config)
GlobalVector globalMomentum() const
static int position[264][3]
Definition: ReadPGInfo.cc:509
#define DEFINE_EDM_PLUGIN(factory, type, name)
eventInfo
add run, event number and lumi section
std::vector< unsigned int > convertParamSel(const std::string &selString) const
Constructor of the full muon geometry.
Definition: AlignableMuon.h:37
unsigned int alignableLabelFromLabel(unsigned int label) const override
alignable label from parameter label (works also for alignable label...)
AlignableToMomentumRangeMap theAlignableToMomentumRangeMap
providing unique ID for alignable, space for param IDs
const AlignableObjectId & objectIdProvider() const
Return tracker alignable object ID provider derived from the tracker&#39;s geometry.
AlignableToIdMap::value_type AlignableToIdPair
IdToAlignableMap theIdToAlignableMap
providing unique ID for alignable, space for param IDs
std::vector< std::string > tokenize(std::string const &input, std::string const &separator)
breaks the input string into tokens, delimited by the separator