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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
/****************************************************************************
* Authors: 
*  Jan Kašpar (jan.kaspar@gmail.com) 
****************************************************************************/

#include "CalibPPS/AlignmentRelative/interface/AlignmentTask.h"
#include "CalibPPS/AlignmentRelative/interface/AlignmentConstraint.h"

#include "DataFormats/CTPPSDetId/interface/CTPPSDetId.h"
#include "DataFormats/CTPPSDetId/interface/TotemRPDetId.h"

#include "Geometry/VeryForwardGeometryBuilder/interface/CTPPSGeometry.h"

#include <algorithm>

//----------------------------------------------------------------------------------------------------

using namespace std;
using namespace edm;

//----------------------------------------------------------------------------------------------------

AlignmentTask::AlignmentTask(const ParameterSet &ps)
    : resolveShR(ps.getParameter<bool>("resolveShR")),
      resolveShZ(ps.getParameter<bool>("resolveShZ")),
      resolveRotZ(ps.getParameter<bool>("resolveRotZ")),

      oneRotZPerPot(ps.getParameter<bool>("oneRotZPerPot")),
      useEqualMeanUMeanVRotZConstraints(ps.getParameter<bool>("useEqualMeanUMeanVRotZConstraints")),

      fixedDetectorsConstraints(ps.getParameterSet("fixedDetectorsConstraints")),
      standardConstraints(ps.getParameterSet("standardConstraints")) {
  if (resolveShR) {
    quantityClasses.push_back(qcShR1);
    quantityClasses.push_back(qcShR2);
  }

  if (resolveShZ) {
    quantityClasses.push_back(qcShZ);
  }

  if (resolveRotZ) {
    quantityClasses.push_back(qcRotZ);
  }
}

//----------------------------------------------------------------------------------------------------

void AlignmentTask::buildGeometry(const vector<unsigned int> &rpDecIds,
                                  const vector<unsigned int> &excludedSensors,
                                  const CTPPSGeometry *input,
                                  double z0,
                                  AlignmentGeometry &geometry) {
  geometry.z0 = z0;

  // traverse full known geometry
  for (auto it = input->beginSensor(); it != input->endSensor(); ++it) {
    // skip excluded sensors
    if (find(excludedSensors.begin(), excludedSensors.end(), it->first) != excludedSensors.end())
      continue;

    // is RP selected?
    const CTPPSDetId detId(it->first);
    const unsigned int rpDecId = 100 * detId.arm() + 10 * detId.station() + detId.rp();
    if (find(rpDecIds.begin(), rpDecIds.end(), rpDecId) == rpDecIds.end())
      continue;

    // extract geometry data
    CTPPSGeometry::Vector c = input->localToGlobal(detId, CTPPSGeometry::Vector(0., 0., 0.));
    CTPPSGeometry::Vector d1 = input->localToGlobal(detId, CTPPSGeometry::Vector(1., 0., 0.)) - c;
    CTPPSGeometry::Vector d2 = input->localToGlobal(detId, CTPPSGeometry::Vector(0., 1., 0.)) - c;

    // for strips: is it U plane?
    bool isU = false;
    if (detId.subdetId() == CTPPSDetId::sdTrackingStrip) {
      TotemRPDetId stripDetId(it->first);
      unsigned int rpNum = stripDetId.rp();
      unsigned int plNum = stripDetId.plane();
      isU = (plNum % 2 != 0);
      if (rpNum == 2 || rpNum == 3)
        isU = !isU;
    }

    DetGeometry dg(c.z() - z0, c.x(), c.y(), isU);
    dg.setDirection(1, d1.x(), d1.y(), d1.z());
    dg.setDirection(2, d2.x(), d2.y(), d2.z());
    geometry.insert(it->first, dg);
  }
}

//----------------------------------------------------------------------------------------------------

void AlignmentTask::buildIndexMaps() {
  // remove old mapping
  mapMeasurementIndeces.clear();
  mapQuantityIndeces.clear();

  // loop over all classes
  for (const auto &qcl : quantityClasses) {
    // create entry for this class
    mapMeasurementIndeces[qcl];

    // loop over all sensors
    unsigned int idxMeas = 0;
    unsigned int idxQuan = 0;
    for (const auto &git : geometry.getSensorMap()) {
      const unsigned int detId = git.first;
      const unsigned int subdetId = CTPPSDetId(git.first).subdetId();

      // update measurement map
      if (qcl == qcShR1) {
        if (subdetId == CTPPSDetId::sdTimingDiamond)
          mapMeasurementIndeces[qcl][{detId, 1}] = idxMeas++;
        if (subdetId == CTPPSDetId::sdTrackingPixel)
          mapMeasurementIndeces[qcl][{detId, 1}] = idxMeas++;
      }

      if (qcl == qcShR2) {
        if (subdetId == CTPPSDetId::sdTrackingStrip)
          mapMeasurementIndeces[qcl][{detId, 2}] = idxMeas++;
        if (subdetId == CTPPSDetId::sdTrackingPixel)
          mapMeasurementIndeces[qcl][{detId, 2}] = idxMeas++;
      }

      if (qcl == qcShZ) {
        if (subdetId == CTPPSDetId::sdTrackingStrip)
          mapMeasurementIndeces[qcl][{detId, 2}] = idxMeas++;
        if (subdetId == CTPPSDetId::sdTrackingPixel)
          mapMeasurementIndeces[qcl][{detId, 1}] = idxMeas++;
        if (subdetId == CTPPSDetId::sdTrackingPixel)
          mapMeasurementIndeces[qcl][{detId, 2}] = idxMeas++;
        if (subdetId == CTPPSDetId::sdTimingDiamond)
          mapMeasurementIndeces[qcl][{detId, 1}] = idxMeas++;
      }

      if (qcl == qcRotZ) {
        if (subdetId == CTPPSDetId::sdTrackingStrip)
          mapMeasurementIndeces[qcl][{detId, 2}] = idxMeas++;
        if (subdetId == CTPPSDetId::sdTrackingPixel)
          mapMeasurementIndeces[qcl][{detId, 1}] = idxMeas++;
        if (subdetId == CTPPSDetId::sdTrackingPixel)
          mapMeasurementIndeces[qcl][{detId, 2}] = idxMeas++;
        if (subdetId == CTPPSDetId::sdTimingDiamond)
          mapMeasurementIndeces[qcl][{detId, 1}] = idxMeas++;
      }

      // update quantity map
      if (qcl == qcShR1) {
        if (subdetId == CTPPSDetId::sdTimingDiamond)
          mapQuantityIndeces[qcl][detId] = idxQuan++;
        if (subdetId == CTPPSDetId::sdTrackingPixel)
          mapQuantityIndeces[qcl][detId] = idxQuan++;
      }

      if (qcl == qcShR2) {
        if (subdetId == CTPPSDetId::sdTrackingStrip)
          mapQuantityIndeces[qcl][detId] = idxQuan++;
        if (subdetId == CTPPSDetId::sdTrackingPixel)
          mapQuantityIndeces[qcl][detId] = idxQuan++;
      }

      if (qcl == qcShZ) {
        if (subdetId == CTPPSDetId::sdTrackingStrip)
          mapQuantityIndeces[qcl][detId] = idxQuan++;
        if (subdetId == CTPPSDetId::sdTimingDiamond)
          mapQuantityIndeces[qcl][detId] = idxQuan++;
        if (subdetId == CTPPSDetId::sdTrackingPixel)
          mapQuantityIndeces[qcl][detId] = idxQuan++;
      }

      if (qcl == qcRotZ) {
        if (subdetId == CTPPSDetId::sdTrackingStrip)
          mapQuantityIndeces[qcl][detId] = idxQuan++;
        if (subdetId == CTPPSDetId::sdTimingDiamond)
          mapQuantityIndeces[qcl][detId] = idxQuan++;
        if (subdetId == CTPPSDetId::sdTrackingPixel)
          mapQuantityIndeces[qcl][detId] = idxQuan++;
      }
    }
  }
}

//----------------------------------------------------------------------------------------------------

signed int AlignmentTask::getMeasurementIndex(QuantityClass cl, unsigned int detId, unsigned int dirIdx) const {
  auto clit = mapMeasurementIndeces.find(cl);
  if (clit == mapMeasurementIndeces.end())
    return -1;

  auto it = clit->second.find({detId, dirIdx});
  if (it == clit->second.end())
    return -1;

  return it->second;
}

//----------------------------------------------------------------------------------------------------

signed int AlignmentTask::getQuantityIndex(QuantityClass cl, unsigned int detId) const {
  auto clit = mapQuantityIndeces.find(cl);
  if (clit == mapQuantityIndeces.end())
    return -1;

  auto it = clit->second.find(detId);
  if (it == clit->second.end())
    return -1;

  return it->second;
}

//----------------------------------------------------------------------------------------------------

string AlignmentTask::quantityClassTag(QuantityClass qc) const {
  switch (qc) {
    case qcShR1:
      return "ShR1";
    case qcShR2:
      return "ShR2";
    case qcShZ:
      return "ShZ";
    case qcRotZ:
      return "RotZ";
  }

  throw cms::Exception("PPS") << "Unknown quantity class " << qc << ".";
}

//----------------------------------------------------------------------------------------------------

unsigned int AlignmentTask::measurementsOfClass(QuantityClass qc) const {
  auto it = mapMeasurementIndeces.find(qc);
  if (it == mapMeasurementIndeces.end())
    return 0;
  else
    return it->second.size();
}

//----------------------------------------------------------------------------------------------------

unsigned int AlignmentTask::quantitiesOfClass(QuantityClass qc) const {
  auto it = mapQuantityIndeces.find(qc);
  if (it == mapQuantityIndeces.end())
    return 0;
  else
    return it->second.size();
}

//----------------------------------------------------------------------------------------------------

void AlignmentTask::buildFixedDetectorsConstraints(vector<AlignmentConstraint> &constraints) const {
  for (auto &quantityClass : quantityClasses) {
    // get input
    const string &tag = quantityClassTag(quantityClass);

    const ParameterSet &classSettings = fixedDetectorsConstraints.getParameterSet(tag.c_str());
    vector<unsigned int> ids(classSettings.getParameter<vector<unsigned int>>("ids"));
    vector<double> values(classSettings.getParameter<vector<double>>("values"));

    if (ids.size() != values.size())
      throw cms::Exception("PPS") << "Different number of constraint ids and values for " << tag << ".";

    // determine number of constraints
    unsigned int size = ids.size();

    // just one basic constraint
    if (oneRotZPerPot && quantityClass == qcRotZ) {
      if (size > 1)
        size = 1;
    }

    // build constraints
    for (unsigned int j = 0; j < size; j++) {
      // prepare empty constraint
      AlignmentConstraint ac;

      for (auto &qcit : quantityClasses) {
        ac.coef[qcit].ResizeTo(quantitiesOfClass(qcit));
        ac.coef[qcit].Zero();
      }

      // set constraint name
      char buf[40];
      sprintf(buf, "%s: fixed plane %4u", tag.c_str(), ids[j]);
      ac.name = buf;

      // get quantity index
      signed int qIndex = getQuantityIndex(quantityClass, ids[j]);
      if (qIndex < 0)
        throw cms::Exception("AlignmentTask::BuildFixedDetectorsConstraints")
            << "Quantity index for class " << quantityClass << " and id " << ids[j] << " is " << qIndex;

      // set constraint coefficient and value
      ac.coef[quantityClass][qIndex] = 1.;
      ac.val = values[j] * 1E-3;

      // save constraint
      constraints.push_back(ac);
    }

    if (oneRotZPerPot && quantityClass == qcRotZ)
      buildOneRotZPerPotConstraints(constraints);
  }
}

//----------------------------------------------------------------------------------------------------

void AlignmentTask::buildStandardConstraints(vector<AlignmentConstraint> &constraints) const {
  const vector<unsigned int> &decUnitIds = standardConstraints.getParameter<vector<unsigned int>>("units");

  // count planes in RPs
  map<unsigned int, unsigned int> planesPerPot;
  for (const auto &it : geometry.getSensorMap()) {
    CTPPSDetId detId(it.first);
    planesPerPot[detId.rpId()]++;
  }

  // ShR constraints
  if (resolveShR) {
    for (const auto &decUnitId : decUnitIds) {
      // prepare empty constraints
      AlignmentConstraint ac_X;
      for (auto &qcit : quantityClasses) {
        ac_X.coef[qcit].ResizeTo(quantitiesOfClass(qcit));
        ac_X.coef[qcit].Zero();
      }
      ac_X.val = 0;

      AlignmentConstraint ac_Y(ac_X);

      // set constraint names
      char buf[50];
      sprintf(buf, "ShR: unit %u, MeanX=0", decUnitId);
      ac_X.name = buf;
      sprintf(buf, "ShR: unit %u, MeanY=0", decUnitId);
      ac_Y.name = buf;

      // traverse geometry
      for (const auto &git : geometry.getSensorMap()) {
        // stop is sensor not in the selected arm
        CTPPSDetId senId(git.first);
        unsigned int senDecUnit = senId.arm() * 100 + senId.station() * 10;
        if (senId.rp() > 2)
          senDecUnit += 1;

        if (senDecUnit != decUnitId)
          continue;

        // fill constraint for strip sensors
        if (senId.subdetId() == CTPPSDetId::sdTrackingStrip) {
          signed int qIndex = getQuantityIndex(qcShR2, git.first);
          if (qIndex < 0)
            throw cms::Exception("AlignmentTask::BuildStandardConstraints")
                << "Cannot get quantity index for class " << qcShR2 << " and sensor id " << git.first << ".";

          // determine weight
          const double weight = 1. / planesPerPot[senId.rpId()];

          // set constraint coefficients
          ac_X.coef[qcShR2][qIndex] = git.second.getDirectionData(2).dx * weight;
          ac_Y.coef[qcShR2][qIndex] = git.second.getDirectionData(2).dy * weight;
        }

        // fill constraint for pixel sensors
        if (senId.subdetId() == CTPPSDetId::sdTrackingPixel) {
          // get quantity indeces
          const signed int qIndex1 = getQuantityIndex(qcShR1, git.first);
          if (qIndex1 < 0)
            throw cms::Exception("AlignmentTask::BuildStandardConstraints")
                << "Cannot get quantity index for class " << qcShR1 << " and sensor id " << git.first << ".";

          const signed int qIndex2 = getQuantityIndex(qcShR2, git.first);
          if (qIndex2 < 0)
            throw cms::Exception("AlignmentTask::BuildStandardConstraints")
                << "Cannot get quantity index for class " << qcShR2 << " and sensor id " << git.first << ".";

          // determine weight (two constraints per plane)
          const double weight = 0.5 / planesPerPot[senId.rpId()];

          // get geometry
          const double d1x = git.second.getDirectionData(1).dx;
          const double d1y = git.second.getDirectionData(1).dy;
          const double d2x = git.second.getDirectionData(2).dx;
          const double d2y = git.second.getDirectionData(2).dy;

          // calculate coefficients, by inversion of this matrix relation
          //  [ s1 ] = [ d1x  d1y ] * [ de x ]
          //  [ s2 ]   [ d2x  d2y ]   [ de y ]
          const double D = d1x * d2y - d1y * d2x;
          const double coef_x_s1 = +d2y / D;
          const double coef_y_s1 = -d2x / D;
          const double coef_x_s2 = -d1y / D;
          const double coef_y_s2 = +d1x / D;

          // set constraint coefficients
          ac_X.coef[qcShR1][qIndex1] = coef_x_s1 * weight;
          ac_Y.coef[qcShR1][qIndex1] = coef_y_s1 * weight;
          ac_X.coef[qcShR2][qIndex2] = coef_x_s2 * weight;
          ac_Y.coef[qcShR2][qIndex2] = coef_y_s2 * weight;
        }
      }

      // add constraints
      constraints.push_back(ac_X);
      constraints.push_back(ac_Y);
    }
  }

  // RotZ constraints
  if (resolveRotZ) {
    for (const auto &decUnitId : decUnitIds) {
      // prepare empty constraints
      AlignmentConstraint ac;
      for (unsigned int i = 0; i < quantityClasses.size(); i++) {
        ac.coef[quantityClasses[i]].ResizeTo(quantitiesOfClass(quantityClasses[i]));
        ac.coef[quantityClasses[i]].Zero();
      }
      ac.val = 0;

      char buf[50];
      sprintf(buf, "RotZ: unit %u, Mean=0", decUnitId);
      ac.name = buf;

      // traverse geometry
      for (const auto &git : geometry.getSensorMap()) {
        // stop is sensor not in the selected arm
        CTPPSDetId senId(git.first);
        unsigned int senDecUnit = senId.arm() * 100 + senId.station() * 10;
        if (senId.rp() > 2)
          senDecUnit += 1;

        if (senDecUnit != decUnitId)
          continue;

        // determine weight
        const double weight = 1. / planesPerPot[senId.rpId()];

        // set coefficient
        signed int qIndex = getQuantityIndex(qcRotZ, git.first);
        ac.coef[qcRotZ][qIndex] = weight;
      }

      constraints.push_back(ac);
    }
  }

  if (resolveRotZ && oneRotZPerPot)
    buildOneRotZPerPotConstraints(constraints);

  if (resolveRotZ && useEqualMeanUMeanVRotZConstraints)
    buildEqualMeanUMeanVRotZConstraints(constraints);
}

//----------------------------------------------------------------------------------------------------

void AlignmentTask::buildOneRotZPerPotConstraints(std::vector<AlignmentConstraint> &constraints) const {
  // build map rp id --> sensor ids
  map<unsigned int, vector<unsigned int>> m;
  for (const auto &p : geometry.getSensorMap()) {
    CTPPSDetId detId(p.first);
    CTPPSDetId rpId = detId.rpId();
    unsigned int decRPId = rpId.arm() * 100 + rpId.station() * 10 + rpId.rp();
    m[decRPId].push_back(p.first);
  }

  // traverse all RPs
  for (const auto &p : m) {
    // build n_planes-1 constraints
    unsigned int prev_detId = 0;
    for (const auto &detId : p.second) {
      if (prev_detId != 0) {
        AlignmentConstraint ac;

        char buf[100];
        sprintf(buf, "RotZ: RP %u, plane %u = plane %u", p.first, prev_detId, detId);
        ac.name = buf;

        ac.val = 0;

        for (auto &qcit : quantityClasses) {
          ac.coef[qcit].ResizeTo(quantitiesOfClass(qcit));
          ac.coef[qcit].Zero();
        }

        signed int qIdx1 = getQuantityIndex(qcRotZ, prev_detId);
        signed int qIdx2 = getQuantityIndex(qcRotZ, detId);

        ac.coef[qcRotZ][qIdx1] = +1.;
        ac.coef[qcRotZ][qIdx2] = -1.;

        constraints.push_back(ac);
      }

      prev_detId = detId;
    }
  }
}

//----------------------------------------------------------------------------------------------------

void AlignmentTask::buildEqualMeanUMeanVRotZConstraints(vector<AlignmentConstraint> &constraints) const {
  // build map strip rp id --> pair( vector of U planes, vector of V planes )
  map<unsigned int, pair<vector<unsigned int>, vector<unsigned int>>> m;
  for (const auto &p : geometry.getSensorMap()) {
    CTPPSDetId detId(p.first);

    if (detId.subdetId() != CTPPSDetId::sdTrackingStrip)
      continue;

    CTPPSDetId rpId = detId.rpId();
    unsigned int decRPId = rpId.arm() * 100 + rpId.station() * 10 + rpId.rp();

    if (p.second.isU)
      m[decRPId].first.push_back(p.first);
    else
      m[decRPId].second.push_back(p.first);
  }

  // loop over RPs
  for (const auto &p : m) {
    AlignmentConstraint ac;

    char buf[100];
    sprintf(buf, "RotZ: RP %u, MeanU = MeanV", p.first);
    ac.name = buf;

    ac.val = 0;

    for (auto &qcit : quantityClasses) {
      ac.coef[qcit].ResizeTo(quantitiesOfClass(qcit));
      ac.coef[qcit].Zero();
    }

    for (auto &&proj : {"U", "V"}) {
      const auto &planes = (proj == string("U")) ? p.second.first : p.second.second;
      const double c = ((proj == string("U")) ? -1. : +1.) / planes.size();

      for (const auto &plane : planes) {
        signed int qIdx = getQuantityIndex(qcRotZ, plane);
        ac.coef[qcRotZ][qIdx] = c;

        TotemRPDetId plId(plane);
      }
    }

    constraints.push_back(ac);
  }
}