Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
#include "Alignment/CommonAlignment/interface/AlignableCompositeBuilder.h"

// Original Author:  Max Stark
//         Created:  Thu, 13 Jan 2016 10:22:57 CET

// core framework functionality
#include "FWCore/MessageLogger/interface/MessageLogger.h"

// alignment
#include "Alignment/CommonAlignment/interface/AlignableObjectId.h"

//=============================================================================
//===   PUBLIC METHOD IMPLEMENTATION                                        ===
//=============================================================================

//_____________________________________________________________________________
AlignableCompositeBuilder ::AlignableCompositeBuilder(const TrackerTopology* trackerTopology,
                                                      const TrackerGeometry* trackerGeometry,
                                                      const AlignableIndexer& alignableIndexer)
    : trackerTopology_(trackerTopology),
      alignableObjectId_(trackerGeometry, nullptr, nullptr, nullptr),
      alignableIndexer_(alignableIndexer) {}

//_____________________________________________________________________________
void AlignableCompositeBuilder ::addAlignmentLevel(std::unique_ptr<AlignmentLevel> level) {
  alignmentLevels_.push_back(std::move(level));
}

//_____________________________________________________________________________
void AlignableCompositeBuilder ::clearAlignmentLevels() { alignmentLevels_.clear(); }

//_____________________________________________________________________________
unsigned int AlignableCompositeBuilder ::buildAll(AlignableMap& alignableMap, bool update) {
  auto highestLevel = alignmentLevels_.back()->levelType;

  std::ostringstream ss;
  ss << "building CompositeAlignables for " << alignableObjectId_.idToString(highestLevel) << "\n";

  unsigned int numCompositeAlignables = 0;
  for (unsigned int level = 1; level < alignmentLevels_.size(); ++level) {
    numCompositeAlignables += buildLevel(level, alignableMap, ss, update);
  }

  ss << "built " << numCompositeAlignables << " CompositeAlignables for "
     << alignableObjectId_.idToString(highestLevel);
  edm::LogInfo("AlignableBuildProcess") << "@SUB=AlignableCompositeBuilder::buildAll" << ss.str();

  return numCompositeAlignables;
}

//=============================================================================
//===   PRIVATE METHOD IMPLEMENTATION                                       ===
//=============================================================================

//_____________________________________________________________________________
unsigned int AlignableCompositeBuilder ::buildLevel(unsigned int parentLevel,
                                                    AlignableMap& alignableMap,
                                                    std::ostringstream& ss,
                                                    bool update) {
  unsigned int childLevel = parentLevel - 1;
  unsigned int maxNumParents = maxNumComponents(parentLevel);

  auto childType = alignmentLevels_[childLevel]->levelType;
  auto parentType = alignmentLevels_[parentLevel]->levelType;

  auto& children = alignableMap.find(alignableObjectId_.idToString(childType));
  auto& parents = alignableMap.get(alignableObjectId_.idToString(parentType));
  if (!update)
    parents.reserve(maxNumParents);

  // This vector is used indicate if a parent already exists. It is initialized
  // with 'naked' Alignables-pointers; if the pointer is not naked (!= nullptr)
  // for one of the child-IDs, its parent was already built before.
  align::Alignables tmpParents(maxNumParents, nullptr);

  for (auto* child : children) {
    // get the number of the child-Alignable ...
    const auto index = getIndexOfStructure(child->id(), parentLevel);
    // ... and use it as index to get the parent of this child
    auto& parent = tmpParents[index];

    // if parent was not built yet ...
    if (!parent && !update) {
      if (update) {
        throw cms::Exception("LogicError") << "@SUB=AlignableCompositeBuilder::buildLevel\n"
                                           << "trying to update a non-existing AlignableComposite";
      }
      // ... build new composite Alignable with ID of child (obviously its the
      // first child of the Alignable)
      if (alignmentLevels_[parentLevel]->isFlat) {
        parent = new AlignableComposite(child->id(), parentType, child->globalRotation());
      } else {
        parent = new AlignableComposite(child->id(), parentType, align::RotationType());
      }
      parents.push_back(parent);
    } else if (update) {
      if (alignmentLevels_[parentLevel]->isFlat) {
        // needed to update rotation of flat composites
        auto mother = dynamic_cast<AlignableComposite*>(child->mother());
        if (!mother) {
          throw cms::Exception("LogicError") << "@SUB=AlignableCompositeBuilder::buildLevel\n"
                                             << "trying to update a flat composite that is not of type "
                                             << "AlignableComposite";
        }
        if (mother->id() == child->id()) {
          mother->update(child->id(), parentType, child->globalRotation());
        }
      }
    }

    // in all cases (except updates) add the child to the parent Alignable
    if (!update)
      parent->addComponent(child);
  }

  ss << "   built " << parents.size() << " " << alignableObjectId_.idToString(alignmentLevels_[parentLevel]->levelType)
     << "(s) (theoretical maximum: " << maxNumParents << ") consisting of " << children.size() << " "
     << alignableObjectId_.idToString(alignmentLevels_[childLevel]->levelType) << "(s)\n";

  return parents.size();
}

//_____________________________________________________________________________
unsigned int AlignableCompositeBuilder ::maxNumComponents(unsigned int startLevel) const {
  unsigned int components = 1;

  for (unsigned int level = startLevel; level < alignmentLevels_.size(); ++level) {
    components *= alignmentLevels_[level]->maxNumComponents;
  }

  return components;
}

//_____________________________________________________________________________
unsigned int AlignableCompositeBuilder ::getIndexOfStructure(align::ID id, unsigned int level) const {
  // indexer returns a function pointer for the structure-type
  auto indexOf = alignableIndexer_.get(alignmentLevels_[level]->levelType, alignableObjectId_);

  if (alignmentLevels_.size() - 1 > level) {
    return getIndexOfStructure(id, level + 1) * alignmentLevels_[level]->maxNumComponents + indexOf(id) - 1;
  }

  return indexOf(id) - 1;
}