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