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
#include "CondTools/RPC/interface/RPCLBLinkNameParser.h"

#include <sstream>
#include <algorithm>
#include "FWCore/Utilities/interface/Exception.h"

void RPCLBLinkNameParser::parse(std::string const& name, RPCLBLink& lb_link) {
  lb_link.reset();
  std::string::size_type size = name.size();
  std::string::size_type pos(0), next(0);
  int tmp;

  std::istringstream conv;

  // region
  pos = name.find("_R", pos);
  if (pos == std::string::npos || (pos += 2) >= size)
    throw cms::Exception("InvalidLinkBoardName") << "Expected _R[region], got " << name;
  switch (name.at(pos)) {
    case 'B':
      lb_link.setRegion(0);
      break;
    case 'E':
      lb_link.setRegion(1);
      break;
    default:
      throw cms::Exception("InvalidLinkBoardName") << "Expected Region B or E, got " << name.at(pos) << " in " << name;
      break;
  }
  if ((++pos) >= size)
    throw cms::Exception("InvalidLinkBoardName") << "Name too short: " << name;

  // yoke
  next = name.find_first_not_of("+-0123456789", pos);
  conv.clear();
  conv.str(name.substr(pos, next - pos));
  conv >> tmp;
  lb_link.setYoke(tmp);
  pos = next;

  // sector
  pos = name.find("_S", pos);
  if (pos == std::string::npos || (pos += 2) >= size)
    throw cms::Exception("InvalidLinkBoardName") << "Expected _S[sector], got " << name;
  next = name.find_first_not_of("+-0123456789", pos);
  conv.clear();
  conv.str(name.substr(pos, next - pos));
  conv >> tmp;
  lb_link.setSector(tmp);
  pos = next;

  // (region) side
  pos = name.find('_', pos);
  if (pos == std::string::npos || (pos += 2) >= size)
    throw cms::Exception("InvalidLinkBoardName") << "Name too short: " << name;
  switch (name.at(pos)) {
    case 'N':
      lb_link.setSide(0);
      break;
    case 'M':
      lb_link.setSide(1);
      break;
    case 'P':
      lb_link.setSide(2);
      break;
    default:
      throw cms::Exception("InvalidLinkBoardName") << "Expected Side N, M or P, got " << name.at(pos) << " in " << name;
      break;
  }
  if ((++pos) >= size)
    throw cms::Exception("InvalidLinkBoardName") << "Name too short: " << name;

  // wheelordisk
  conv.clear();
  conv.str(name.substr(pos, 1));
  conv >> tmp;
  lb_link.setWheelOrDisk(tmp);
  if ((++pos) >= size)
    throw cms::Exception("InvalidLinkBoardName") << "Name too short: " << name;

  // fibre
  {
    std::string fibre("123ABCDE");
    char const* tmpchar = std::find(&(fibre[0]), &(fibre[0]) + 8, name.at(pos));
    lb_link.setFibre(tmpchar - &(fibre[0]));
  }
  if ((++pos) >= size)
    return;

  // radial
  next = name.find("_CH", pos);
  if (next == std::string::npos)
    next = size;
  if (next - pos == 2) {
    std::string radial = name.substr(pos, 2);
    if (radial == "ab")
      lb_link.setRadial(0);
    else if (radial == "cd")
      lb_link.setRadial(1);
  }

  if (next == size)
    return;

  // linkboard
  pos = next;
  if (pos + 3 >= size)
    throw cms::Exception("InvalidLinkBoardName") << "Name too short: " << name;
  pos += 3;
  next = name.find_first_not_of("+-0123456789", pos);
  conv.clear();
  conv.str(name.substr(pos, next - pos));
  conv >> tmp;
  lb_link.setLinkBoard(tmp);
}

RPCLBLink RPCLBLinkNameParser::parse(std::string const& name) {
  RPCLBLink lb_link;
  parse(name, lb_link);
  return lb_link;
}