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

#include <fstream>
#include <memory>
#include <sstream>

#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ParameterSet/interface/FileInPath.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/Exception.h"

#include "CondTools/RPC/interface/RPCLBLinkNameParser.h"

RPCAMCLinkMapHandler::RPCAMCLinkMapHandler(edm::ParameterSet const& config)
    : id_(config.getParameter<std::string>("identifier")),
      data_tag_(config.getParameter<std::string>("dataTag")),
      since_run_(config.getParameter<unsigned long long>("sinceRun")),
      input_file_(config.getParameter<edm::FileInPath>("inputFile").fullPath()),
      wheel_not_side_(config.getParameter<bool>("wheelNotSide")),
      wos_fed_(config.getParameter<std::vector<int> >("wheelOrSideFED")),
      n_sectors_(config.getParameter<unsigned int>("nSectors")),
      wos_sector_amc_(wos_fed_.size(), std::vector<int>(n_sectors_, 0)),
      txt_file_(config.getUntrackedParameter<std::string>("txtFile", "")) {
  std::vector<long long> wos_sector_amc_packed(config.getParameter<std::vector<long long> >("wheelOrSideSectorAMC"));

  if (wos_fed_.size() != wos_sector_amc_packed.size()) {
    throw cms::Exception("RPCAMCLinkMapHandler") << "Refuse to handle inconsistent input: "
                                                 << "sizes of wheelOrSideFED and wheelOrSideSectorAMC don't match";
  }

  std::vector<std::vector<int> >::iterator sector_amc = wos_sector_amc_.begin();
  for (std::vector<long long>::const_iterator sector_amc_packed = wos_sector_amc_packed.begin();
       sector_amc_packed != wos_sector_amc_packed.end();
       ++sector_amc_packed, ++sector_amc) {
    for (unsigned int sector = 0; sector < n_sectors_; ++sector) {
      sector_amc->at(sector) = ((*sector_amc_packed) >> (4 * (n_sectors_ - sector - 1))) & 0xf;
    }
  }
}

RPCAMCLinkMapHandler::~RPCAMCLinkMapHandler() {}

void RPCAMCLinkMapHandler::getNewObjects() {
  edm::LogInfo("RPCAMCLinkMapHandler") << "getNewObjects";
  cond::TagInfo_t const& tag_info = tagInfo();
  if (since_run_ < tag_info.lastInterval.since) {
    throw cms::Exception("RPCAMCLinkMapHandler") << "Refuse to create RPCAMCLinkMap for run " << since_run_
                                                 << ", older than most recent tag" << tag_info.lastInterval.since;
  }

  std::string amc_name, link_name;
  unsigned int wos, sector, amc_number, amc_input;

  std::unique_ptr<RPCAMCLinkMap> amc_link_map_object(new RPCAMCLinkMap());
  RPCAMCLinkMap::map_type& amc_link_map = amc_link_map_object->getMap();
  RPCLBLink lb_link;

  std::string line;
  std::istringstream conv;

  std::ifstream input_file(input_file_);

  input_file >> amc_name >> amc_input >> link_name;
  std::getline(input_file, line);

  while (input_file) {
    // parse AMC Slot Name - no checking: failure is an error
    if (wheel_not_side_) {                     // wheel
      std::string::size_type pos(2), next(2);  // skip YB
      int wheel;
      next = amc_name.find_first_not_of("+-0123456789", pos);
      conv.clear();
      conv.str(amc_name.substr(pos, next - pos));
      conv >> wheel;
      wos = wheel + 2;

      pos = next + 2;
      next = amc_name.find_first_not_of("+-0123456789", pos);
      if (next == std::string::npos)
        next = amc_name.size();
      conv.clear();
      conv.str(amc_name.substr(pos, next - pos));
      conv >> sector;
    } else {                                  // side
      wos = (amc_name.at(4) == 'n' ? 0 : 1);  // skip CPPF or OMTF
      conv.clear();
      conv.str(amc_name.substr(5, 1));
      conv >> sector;
    }

    if (sector > n_sectors_) {
      throw cms::Exception("RPCAMCLinkMapHandler")
          << "Found sector greater than the number of sectors: " << sector << " > " << n_sectors_;
    }

    if (wos >= wos_fed_.size()) {
      throw cms::Exception("RPCAMCLinkMapHandler")
          << "Found " << (wheel_not_side_ ? "wheel" : "side") << " outside range: " << wos << " >= " << wos_fed_.size();
    }

    amc_number = wos_sector_amc_.at(wos).at(sector - 1);

    RPCLBLinkNameParser::parse(link_name, lb_link);

    amc_link_map.insert(std::pair<RPCAMCLink, RPCLBLink>(RPCAMCLink(wos_fed_.at(wos), amc_number, amc_input), lb_link));

    input_file >> amc_name >> amc_input >> link_name;
    std::getline(input_file, line);
  }
  input_file.close();

  if (!txt_file_.empty()) {
    edm::LogInfo("RPCAMCLinkMapHandler") << "Fill txtFile";
    std::ofstream ofstream(txt_file_);
    for (auto const link : amc_link_map) {
      ofstream << link.first << ": " << link.second << std::endl;
    }
  }

  edm::LogInfo("RPCAMCLinkMapHandler") << "Add to transfer list";
  m_to_transfer.push_back(std::make_pair(amc_link_map_object.release(), since_run_));
}

std::string RPCAMCLinkMapHandler::id() const { return id_; }