CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MomentumDependentPedeLabeler.cc
Go to the documentation of this file.
1 
11 #include <algorithm>
12 
15 
22 
24 
25 //___________________________________________________________________________
28  :PedeLabelerBase(alignables, config),
29  theOpenMomentumRange(std::pair<float,float>(0.0, 10000.0))
30 {
31  std::vector<Alignable*> alis;
32  alis.push_back(alignables.aliTracker_);
33  alis.push_back(alignables.aliMuon_);
34 
35  if (alignables.aliExtras_) {
36  align::Alignables allExtras = alignables.aliExtras_->components();
37  for ( std::vector<Alignable*>::iterator it = allExtras.begin(); it != allExtras.end(); ++it ) {
38  alis.push_back(*it);
39  }
40  }
41 
42  this->buildMomentumDependencyMap(alignables.aliTracker_,
43  alignables.aliMuon_,
44  alignables.aliExtras_,
45  config);
46  this->buildMap(alis);
47 }
48 
49 //___________________________________________________________________________
50 
52 {
53 }
54 
55 //___________________________________________________________________________
58 {
59  if (!alignable) return 0;
60 
61  AlignableToIdMap::const_iterator position = theAlignableToIdMap.find(alignable);
62  if (position != theAlignableToIdMap.end()) {
63  return position->second;
64  } else {
65  const DetId detId(alignable->id());
66  //throw cms::Exception("LogicError")
67  edm::LogError("LogicError")
68  << "@SUB=MomentumDependentPedeLabeler::alignableLabel" << "Alignable "
69  << typeid(*alignable).name() << " not in map, det/subdet/alignableStructureType = "
70  << detId.det() << "/" << detId.subdetId() << "/" << alignable->alignableObjectId();
71  return 0;
72  }
73 }
74 
75 //___________________________________________________________________________
76 // Return 32-bit unique label for alignable, 0 indicates failure.
78  unsigned int param,
79  unsigned int instance) const
80 {
81  if (!alignable) return 0;
82 
83  AlignableToIdMap::const_iterator position = theAlignableToIdMap.find(alignable);
84  if (position != theAlignableToIdMap.end()) {
85  AlignableToMomentumRangeMap::const_iterator positionAli = theAlignableToMomentumRangeMap.find(alignable);
86  if (positionAli != theAlignableToMomentumRangeMap.end()) {
87  MomentumRangeParamMap::const_iterator positionParam = (*positionAli).second.find(param);
88  if (positionParam!=(*positionAli).second.end()) {
89  if (instance>=(*positionParam).second.size()) {
90  throw cms::Exception("Alignment") << "@SUB=MomentumDependentPedeLabeler::alignableLabelFromParamAndMomentum"
91  << "iovIdx out of bounds";
92  }
93  return position->second + instance * theParamInstanceOffset;
94  } else {
95  return position->second;
96  }
97  } else {
98  return position->second;
99  }
100  } else {
101  const DetId detId(alignable->id());
102  //throw cms::Exception("LogicError")
103  edm::LogError("LogicError")
104  << "@SUB=MomentumDependentPedeLabeler::alignableLabel" << "Alignable "
105  << typeid(*alignable).name() << " not in map, det/subdet/alignableStructureType = "
106  << detId.det() << "/" << detId.subdetId() << "/" << alignable->alignableObjectId();
107  return 0;
108  }
109 }
110 
111 //_________________________________________________________________________
112 unsigned int MomentumDependentPedeLabeler::lasBeamLabel(unsigned int lasBeamId) const
113 {
114  UintUintMap::const_iterator position = theLasBeamToLabelMap.find(lasBeamId);
115  if (position != theLasBeamToLabelMap.end()) {
116  return position->second;
117  } else {
118  //throw cms::Exception("LogicError")
119  edm::LogError("LogicError") << "@SUB=MomentumDependentPedeLabeler::lasBeamLabel"
120  << "No label for beam Id " << lasBeamId;
121  return 0;
122  }
123 }
124 
125 //_________________________________________________________________________
126 unsigned int MomentumDependentPedeLabeler::parameterLabel(unsigned int aliLabel, unsigned int parNum) const
127 {
128  if (parNum >= theMaxNumParam) {
129  throw cms::Exception("Alignment") << "@SUB=MomentumDependentPedeLabeler::parameterLabel"
130  << "Parameter number " << parNum
131  << " out of range 0 <= num < " << theMaxNumParam;
132  }
133  return aliLabel + parNum;
134 }
135 
136 //_________________________________________________________________________
137 unsigned int MomentumDependentPedeLabeler::parameterLabel(Alignable *alignable, unsigned int parNum,
139  const TrajectoryStateOnSurface &tsos) const
140 {
141  if (!alignable) return 0;
142 
143  if (parNum >= theMaxNumParam) {
144  throw cms::Exception("Alignment") << "@SUB=MomentumDependentPedeLabeler::parameterLabel"
145  << "Parameter number " << parNum
146  << " out of range 0 <= num < " << theMaxNumParam;
147  }
148 
149  AlignableToIdMap::const_iterator position = theAlignableToIdMap.find(alignable);
150  if (position != theAlignableToIdMap.end()) {
151 
152  AlignableToMomentumRangeMap::const_iterator positionAli = theAlignableToMomentumRangeMap.find(alignable);
153  if (positionAli != theAlignableToMomentumRangeMap.end()) {
154 
155  MomentumRangeParamMap::const_iterator positionParam = (*positionAli).second.find(parNum);
156  if (positionParam!=(*positionAli).second.end()) {
157 
158  int offset = 0;
159  float mom = tsos.globalMomentum().mag();
160  const MomentumRangeVector & momentumRanges = (*positionParam).second;
161  for (MomentumRangeVector::const_iterator iMomentum = momentumRanges.begin();
162  iMomentum != momentumRanges.end();
163  ++iMomentum) {
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 (MomentumRangeParamMap::const_iterator iParam = (*positionAli).second.begin();
211  iParam != (*positionAli).second.end();
212  ++iParam) {
213  nMomentums = std::max(nMomentums, iParam->second.size());
214  }
215  return nMomentums;
216  } else {
217  MomentumRangeParamMap::const_iterator iParam = (*positionAli).second.find(param);
218  if (iParam != (*positionAli).second.end()) {
219  return iParam->second.size();
220  } else {
221  return 1;
222  }
223  }
224  }
225 
226  return 1;
227 }
228 
229 //___________________________________________________________________________
230 unsigned int MomentumDependentPedeLabeler::paramNumFromLabel(unsigned int paramLabel) const
231 {
232  if (paramLabel < theMinLabel) {
233  edm::LogError("LogicError") << "@SUB=MomentumDependentPedeLabeler::paramNumFromLabel"
234  << "Label " << paramLabel << " should be >= " << theMinLabel;
235  return 0;
236  }
237  return (paramLabel - theMinLabel) % theParamInstanceOffset;
238 }
239 
240 //___________________________________________________________________________
241 unsigned int MomentumDependentPedeLabeler::alignableLabelFromLabel(unsigned int paramLabel) const
242 {
243  return paramLabel - this->paramNumFromLabel(paramLabel);
244 }
245 
246 //___________________________________________________________________________
248 {
249  const unsigned int aliLabel = this->alignableLabelFromLabel(label);
250  if (aliLabel < theMinLabel) return 0; // error already given
251 
252  if (theIdToAlignableMap.empty()) const_cast<MomentumDependentPedeLabeler*>(this)->buildReverseMap();
253  IdToAlignableMap::const_iterator position = theIdToAlignableMap.find(aliLabel);
254  if (position != theIdToAlignableMap.end()) {
255  return position->second;
256  } else {
257  // error only if not in lasBeamMap:
258  UintUintMap::const_iterator position = theLabelToLasBeamMap.find(aliLabel);
259  if (position == theLabelToLasBeamMap.end()) {
260  edm::LogError("LogicError") << "@SUB=MomentumDependentPedeLabeler::alignableFromLabel"
261  << "Alignable label " << aliLabel << " not in map.";
262  }
263  return 0;
264  }
265 }
266 
267 //___________________________________________________________________________
269 {
270  const unsigned int aliLabel = this->alignableLabelFromLabel(label);
271  if (aliLabel < theMinLabel) return 0; // error already given
272 
273  if (theLabelToLasBeamMap.empty()) const_cast<MomentumDependentPedeLabeler*>(this)->buildReverseMap();
274  UintUintMap::const_iterator position = theLabelToLasBeamMap.find(aliLabel);
275  if (position != theLabelToLasBeamMap.end()) {
276  return position->second;
277  } else {
278  edm::LogError("LogicError") << "@SUB=MomentumDependentPedeLabeler::lasBeamIdFromLabel"
279  << "Alignable label " << aliLabel << " not in map.";
280  return 0;
281  }
282 }
283 
284 //__________________________________________________________________________________________________
285 std::vector<std::string> MomentumDependentPedeLabeler::decompose(const std::string &s,
286  std::string::value_type delimiter) const
287 {
288  std::vector<std::string> result;
289 
290  std::string::size_type previousPos = 0;
291  while (true) {
292  const std::string::size_type delimiterPos = s.find(delimiter, previousPos);
293  if (delimiterPos == std::string::npos) {
294  result.push_back(s.substr(previousPos)); // until end
295  break;
296  }
297  result.push_back(s.substr(previousPos, delimiterPos - previousPos));
298  previousPos = delimiterPos + 1; // +1: skip delimiter
299  }
300 
301  return result;
302 }
303 
304 //__________________________________________________________________________________________________
305 std::vector<unsigned int> MomentumDependentPedeLabeler::convertParamSel(const std::string &selString) const
306 {
307  std::vector<unsigned int> result;
308  for (std::string::size_type pos = 0; pos < selString.size(); ++pos) {
309  if (selString[pos]=='1') result.push_back(pos);
310  }
311  return result;
312 }
313 
315  AlignableMuon* aliMuon,
316  AlignableExtras *aliExtras,
317  const edm::ParameterSet &config)
318 {
320 
321  AlignmentParameterSelector selector(aliTracker, aliMuon, aliExtras);
322 
323  std::vector<char> paramSelDumthe(6, '1');
324 
325  const std::vector<edm::ParameterSet> parameterInstancesVPSet =
326  config.getParameter<std::vector<edm::ParameterSet> >("parameterInstances");
327 
328  for (std::vector<edm::ParameterSet>::const_iterator iter = parameterInstancesVPSet.begin();
329  iter != parameterInstancesVPSet.end();
330  ++iter) {
331 
332  const std::vector<std::string> tempMomentumRanges = (*iter).getParameter<std::vector<std::string> >("momentumRanges");
333  if (tempMomentumRanges.size()==0) {
334  throw cms::Exception("BadConfig") << "@SUB=MomentumDependentPedeLabeler::buildMomentumDependencyMap\n"
335  << "MomentumRanges empty\n";
336  }
337 
338  MomentumRangeVector MomentumRanges;
339  float lower;
340  float upper;
341  for (unsigned int iMomentum=0;iMomentum<tempMomentumRanges.size();++iMomentum) {
342  std::vector<std::string> tokens = edm::tokenize(tempMomentumRanges[iMomentum], ":");
343 
344  lower = strtod(tokens[0].c_str(), 0);
345  upper = strtod(tokens[1].c_str(), 0);
346 
347  MomentumRanges.push_back(std::pair<float,float>(lower, upper));
348  }
349 
350  const std::vector<std::string> selStrings = (*iter).getParameter<std::vector<std::string> >("selector");
351  for (std::vector<std::string>::const_iterator iSel = selStrings.begin();
352  iSel != selStrings.end();
353  ++iSel) {
354  std::vector<std::string> decompSel(this->decompose(*iSel, ','));
355 
356  if (decompSel.size()!=2) {
357  throw cms::Exception("BadConfig") << "@SUB=MomentumDependentPedeLabeler::buildMomentumDependencyMap\n"
358  << *iSel <<" should have at least 2 ','-separated parts\n";
359  }
360 
361  std::vector<unsigned int> selParam = this->convertParamSel(decompSel[1]);
362  selector.clear();
363  selector.addSelection(decompSel[0], paramSelDumthe);
364 
365  const std::vector<Alignable*> &alis = selector.selectedAlignables();
366  for (std::vector<Alignable*>::const_iterator iAli = alis.begin();
367  iAli != alis.end();
368  ++iAli) {
369  for (std::vector<unsigned int>::const_iterator iParam = selParam.begin();
370  iParam != selParam.end();
371  ++iParam) {
372  AlignableToMomentumRangeMap::const_iterator positionAli = theAlignableToMomentumRangeMap.find(*iAli);
373  if (positionAli!=theAlignableToMomentumRangeMap.end()) {
374 
375  AlignmentParameters *AliParams = (*positionAli).first->alignmentParameters();
376  if (static_cast<int>(selParam[selParam.size()-1]) >= AliParams->size()) {
377  throw cms::Exception("BadConfig") << "@SUB=MomentumDependentPedeLabeler::buildMomentumDependencyMap\n"
378  << "mismatch in number of parameters\n";
379  }
380 
381  MomentumRangeParamMap::const_iterator positionParam = (*positionAli).second.find(*iParam);
382  if (positionParam!=(*positionAli).second.end()) {
383  throw cms::Exception("BadConfig") << "@SUB=MomentumDependentPedeLabeler::buildMomentumDependencyMap\n"
384  << "Momentum range for parameter specified twice\n";
385  }
386  }
387 
388  theAlignableToMomentumRangeMap[*iAli][*iParam] = MomentumRanges;
389  }
390  }
391  }
392  }
393 
394  return theAlignableToMomentumRangeMap.size();
395 }
396 
397 //_________________________________________________________________________
398 unsigned int MomentumDependentPedeLabeler::buildMap(const std::vector<Alignable*> &alis)
399 {
400  theAlignableToIdMap.clear(); // just in case of re-use...
401 
402  std::vector<Alignable*> allComps;
403 
404  for (std::vector<Alignable*>::const_iterator iAli = alis.begin(); iAli != alis.end(); ++iAli) {
405  if (*iAli) {
406  allComps.push_back(*iAli);
407  (*iAli)->recursiveComponents(allComps);
408  }
409  }
410 
411  unsigned int id = theMinLabel;
412  for (std::vector<Alignable*>::const_iterator iter = allComps.begin();
413  iter != allComps.end(); ++iter) {
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  // return combined size
436  return theAlignableToIdMap.size() + theLasBeamToLabelMap.size();
437 }
438 
439 //_________________________________________________________________________
441 {
442  // alignables
443  theIdToAlignableMap.clear(); // just in case of re-use...
444 
445  for (AlignableToIdMap::iterator it = theAlignableToIdMap.begin();
446  it != theAlignableToIdMap.end(); ++it) {
447  const unsigned int key = (*it).second;
448  Alignable *ali = (*it).first;
449  const unsigned int nInstances = this->numberOfParameterInstances(ali, -1);
450  for (unsigned int iInstance=0;iInstance<nInstances;++iInstance) {
451  theIdToAlignableMap[key+iInstance*theParamInstanceOffset] = ali;
452  }
453  }
454 
455  // las beams
456  theLabelToLasBeamMap.clear(); // just in case of re-use...
457 
458  for (UintUintMap::const_iterator it = theLasBeamToLabelMap.begin();
459  it != theLasBeamToLabelMap.end(); ++it) {
460  theLabelToLasBeamMap[it->second] = it->first; //revert key/value
461  }
462 
463  // return combined size
464  return theIdToAlignableMap.size() + theLabelToLasBeamMap.size();
465 }
466 
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:180
Alignable * alignableFromLabel(unsigned int label) const
unsigned int lasBeamLabel(unsigned int lasBeamId) const
static PFTauRenderPlugin instance
std::vector< std::string > decompose(const std::string &s, std::string::value_type delimiter) const
UintUintMap theLabelToLasBeamMap
labels for las beams
static const unsigned int theMinLabel
unsigned int alignableLabelFromParamAndInstance(Alignable *alignable, unsigned int param, unsigned int instance) const
uint16_t size_type
unsigned int buildMap(const std::vector< Alignable * > &alis)
returns size of map
MomentumDependentPedeLabeler(const PedeLabelerBase::TopLevelAlignables &alignables, const edm::ParameterSet &config)
constructor from three Alignables (null pointers allowed )
static int position[TOTALCHAMBERS][3]
Definition: ReadPGInfo.cc:509
static const unsigned int theMaxNumParam
U second(std::pair< T, U > const &p)
unsigned int paramNumFromLabel(unsigned int paramLabel) const
parameter number, 0 &lt;= .. &lt; theMaxNumParam, belonging to unique parameter label
void clear()
remove all selected Alignables and geometrical restrictions
T mag() const
Definition: PV3DBase.h:66
unsigned int numberOfParameterInstances(Alignable *alignable, int param=-1) const
returns the number of instances for a given parameter
const T & max(const T &a, const T &b)
tuple result
Definition: query.py:137
virtual StructureType alignableObjectId() const =0
Return the alignable type identifier.
bool hasSplitParameters(Alignable *alignable) const
returns true if the alignable has parameters that are split into various bins
unsigned int offset(bool)
Container::value_type value_type
UintUintMap theLasBeamToLabelMap
reverse map
unsigned int buildReverseMap()
returns size of map
unsigned int parameterLabel(unsigned int aliLabel, unsigned int parNum) const
returns the label for a given alignable parameter number combination
unsigned int lasBeamIdFromLabel(unsigned int label) const
Alignables components() const
const align::Alignables & selectedAlignables() const
vector of alignables selected so far
Definition: DetId.h:20
int size(void) const
Get number of parameters.
std::vector< std::string > tokenize(std::string const &input, std::string const &separator)
breaks the input string into tokens, delimited by the separator
Definition: Parse.cc:57
static const unsigned int theParamInstanceOffset
std::vector< Alignable * > Alignables
Definition: Utilities.h:28
std::vector< MomentumRange > MomentumRangeVector
unsigned int addSelection(const std::string &name, const std::vector< char > &paramSel)
unsigned int buildMomentumDependencyMap(AlignableTracker *aliTracker, AlignableMuon *aliMuon, AlignableExtras *extras, const edm::ParameterSet &config)
list key
Definition: combine.py:13
GlobalVector globalMomentum() const
unsigned int alignableLabel(Alignable *alignable) const
Return 32-bit unique label for alignable, 0 indicates failure.
#define DEFINE_EDM_PLUGIN(factory, type, name)
std::vector< unsigned int > convertParamSel(const std::string &selString) const
Constructor of the full muon geometry.
Definition: AlignableMuon.h:36
AlignableToMomentumRangeMap theAlignableToMomentumRangeMap
providing unique ID for alignable, space for param IDs
define event information passed to algorithms
AlignableToIdMap::value_type AlignableToIdPair
unsigned int alignableLabelFromLabel(unsigned int label) const
alignable label from parameter label (works also for alignable label...)
IdToAlignableMap theIdToAlignableMap
providing unique ID for alignable, space for param IDs