<commit_before>/**
 *  @copyright Copyright 2017 The J-PET Framework Authors. All rights reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may find a copy of the License in the LICENCE file.
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  @file JPetGeomMapping.cpp
 */

#include "./JPetGeomMapping.h"

using namespace std;

const size_t JPetGeomMapping::kBadLayerNumber = 99999999;
const size_t JPetGeomMapping::kBadSlotNumber = 99999999;

JPetGeomMapping::JPetGeomMapping(const JPetParamBank& paramBank)
{
  vector<double> layersRadii;
  vector<vector<double> > slotsTheta;
  for (auto & layer : paramBank.getLayers() ) {
    double radius = layer.second->getRadius();
    layersRadii.push_back(radius);
    fNumberOfSlotsInLayer.push_back(0);
    map<double, int> slots_map;
    fThetaToSlot.push_back(slots_map);
    vector<double> slots_theta;
    slotsTheta.push_back(slots_theta);
  }
  sort( layersRadii.begin(), layersRadii.end(), less<double>() );
  int layer_counter = 1;
  for (const auto & radius : layersRadii ) {
    fRadiusToLayer[ radius ] = layer_counter++;
  }
  for (const auto & slot : paramBank.getBarrelSlots()) {
    int layer_number = getLayerNumber( slot.second->getLayer() );
    fNumberOfSlotsInLayer[layer_number - 1]++;
    slotsTheta[layer_number - 1].push_back(slot.second->getTheta());
  }
  vector<int> slotCounters;
  int layerNumber = 1;
  for (auto & thetas : slotsTheta) {
    slotCounters.push_back(1);
    sort( thetas.begin(), thetas.end(), less<double>() );
    for (const double & theta : thetas) {
      fThetaToSlot[layerNumber - 1][theta] = slotCounters[layerNumber - 1]++;
    }
    layerNumber++;
  }
  fTOMBs = getTOMBMap(paramBank);
}

JPetGeomMapping::~JPetGeomMapping() {}

const size_t JPetGeomMapping::getLayersCount() const
{
  return fRadiusToLayer.size();
}

const size_t JPetGeomMapping::getLayerNumber(const JPetLayer& layer) const
{
  auto radius = layer.getRadius();
  if (fRadiusToLayer.find(radius) != fRadiusToLayer.end()) {
    return fRadiusToLayer.at(radius);
  } else {
    ERROR("No layer for radius:" + std::to_string(radius) + " found.");
    return JPetGeomMapping::kBadLayerNumber;
  }
}

const size_t JPetGeomMapping::getSlotsCount(const JPetLayer& layer) const
{
  auto layerNr = getLayerNumber(layer);
  if (fNumberOfSlotsInLayer.size() > layerNr) {
    return fNumberOfSlotsInLayer.at(layerNr - 1);
  } else {
    ERROR("No slot counts found for layer:" + std::to_string(layerNr));
    return JPetGeomMapping::kBadLayerNumber;
  }
}

const size_t JPetGeomMapping::getSlotsCount(const size_t layerNr) const
{
  if ((fNumberOfSlotsInLayer.size() >= layerNr) && (layerNr > 0)) {
    return fNumberOfSlotsInLayer.at(layerNr - 1);
  } else {
    ERROR("No slot counts found for layer:" + std::to_string(layerNr));
    return JPetGeomMapping::kBadSlotNumber;
  }
}

const size_t JPetGeomMapping::getSlotNumber(const JPetBarrelSlot& slot) const
{
  auto layerNr = getLayerNumber(slot.getLayer());
  auto theta = slot.getTheta();
  if ((fNumberOfSlotsInLayer.size() <  layerNr) || (layerNr <= 0)) {
    ERROR("No slot counts found for layer:" + std::to_string(layerNr));
    return JPetGeomMapping::kBadSlotNumber;
  }
  auto index = layerNr - 1;
  if (fThetaToSlot.at(index).find(theta) == fThetaToSlot.at(index).end()) {
    ERROR("No slot number found for theta :" + std::to_string(theta) + " in layer:" + std::to_string(layerNr));
    return JPetGeomMapping::kBadSlotNumber;
  }
  return fThetaToSlot.at(index).at(theta);
}

const size_t JPetGeomMapping::getGlobalSlotNumber(const JPetBarrelSlot& slot) const
{
  auto layerNr = getLayerNumber(slot.getLayer());
  auto index = layerNr - 1;
  auto previousSlots = 0;
  /// We add all previously defined slots in previous layers
  for (auto i = 0u; i < fNumberOfSlotsInLayer.size(); i++) {
    if (index <= i) break; /// if we reach our layer
    previousSlots =  i + fNumberOfSlotsInLayer[i];
  }
  auto slotNrInLayer = getSlotNumber(slot);
  if (slotNrInLayer == JPetGeomMapping::kBadSlotNumber) {
    return JPetGeomMapping::kBadSlotNumber;
  } else {
    return previousSlots + getSlotNumber(slot);
  }
}

const StripPos JPetGeomMapping::getStripPos(const JPetBarrelSlot& slot)const
{
  return {.layer = getLayerNumber(slot.getLayer()), .slot = getSlotNumber(slot)};
}

const vector< size_t > JPetGeomMapping::getLayersSizes() const
{
  vector<size_t> res;
  for (size_t i = 1, n = getLayersCount(); i <= n; i++)
    res.push_back(getSlotsCount(i));
  return res;
}

const size_t JPetGeomMapping::calcDeltaID(const JPetBarrelSlot& slot1, const JPetBarrelSlot& slot2) const
{
  if (slot1.getLayer().getID() == slot2.getLayer().getID()) {
    int delta_ID = abs(getSlotNumber(slot1) - getSlotNumber(slot2));
    int layer_size = getSlotsCount(slot1.getLayer());
    int half_layer_size = layer_size / 2;
    if ( delta_ID > half_layer_size ) return layer_size - delta_ID;
    return delta_ID;<commit_msg>Remove bug from GetGlobalSlotNumber()<commit_after>/**
 *  @copyright Copyright 2017 The J-PET Framework Authors. All rights reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may find a copy of the License in the LICENCE file.
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  @file JPetGeomMapping.cpp
 */

#include "./JPetGeomMapping.h"

using namespace std;

const size_t JPetGeomMapping::kBadLayerNumber = 99999999;
const size_t JPetGeomMapping::kBadSlotNumber = 99999999;

JPetGeomMapping::JPetGeomMapping(const JPetParamBank& paramBank)
{
  vector<double> layersRadii;
  vector<vector<double> > slotsTheta;
  for (auto & layer : paramBank.getLayers() ) {
    double radius = layer.second->getRadius();
    layersRadii.push_back(radius);
    fNumberOfSlotsInLayer.push_back(0);
    map<double, int> slots_map;
    fThetaToSlot.push_back(slots_map);
    vector<double> slots_theta;
    slotsTheta.push_back(slots_theta);
  }
  sort( layersRadii.begin(), layersRadii.end(), less<double>() );
  int layer_counter = 1;
  for (const auto & radius : layersRadii ) {
    fRadiusToLayer[ radius ] = layer_counter++;
  }
  for (const auto & slot : paramBank.getBarrelSlots()) {
    int layer_number = getLayerNumber( slot.second->getLayer() );
    fNumberOfSlotsInLayer[layer_number - 1]++;
    slotsTheta[layer_number - 1].push_back(slot.second->getTheta());
  }
  vector<int> slotCounters;
  int layerNumber = 1;
  for (auto & thetas : slotsTheta) {
    slotCounters.push_back(1);
    sort( thetas.begin(), thetas.end(), less<double>() );
    for (const double & theta : thetas) {
      fThetaToSlot[layerNumber - 1][theta] = slotCounters[layerNumber - 1]++;
    }
    layerNumber++;
  }
  fTOMBs = getTOMBMap(paramBank);
}

JPetGeomMapping::~JPetGeomMapping() {}

const size_t JPetGeomMapping::getLayersCount() const
{
  return fRadiusToLayer.size();
}

const size_t JPetGeomMapping::getLayerNumber(const JPetLayer& layer) const
{
  auto radius = layer.getRadius();
  if (fRadiusToLayer.find(radius) != fRadiusToLayer.end()) {
    return fRadiusToLayer.at(radius);
  } else {
    ERROR("No layer for radius:" + std::to_string(radius) + " found.");
    return JPetGeomMapping::kBadLayerNumber;
  }
}

const size_t JPetGeomMapping::getSlotsCount(const JPetLayer& layer) const
{
  auto layerNr = getLayerNumber(layer);
  if (fNumberOfSlotsInLayer.size() > layerNr) {
    return fNumberOfSlotsInLayer.at(layerNr - 1);
  } else {
    ERROR("No slot counts found for layer:" + std::to_string(layerNr));
    return JPetGeomMapping::kBadLayerNumber;
  }
}

const size_t JPetGeomMapping::getSlotsCount(const size_t layerNr) const
{
  if ((fNumberOfSlotsInLayer.size() >= layerNr) && (layerNr > 0)) {
    return fNumberOfSlotsInLayer.at(layerNr - 1);
  } else {
    ERROR("No slot counts found for layer:" + std::to_string(layerNr));
    return JPetGeomMapping::kBadSlotNumber;
  }
}

const size_t JPetGeomMapping::getSlotNumber(const JPetBarrelSlot& slot) const
{
  auto layerNr = getLayerNumber(slot.getLayer());
  auto theta = slot.getTheta();
  if ((fNumberOfSlotsInLayer.size() <  layerNr) || (layerNr <= 0)) {
    ERROR("No slot counts found for layer:" + std::to_string(layerNr));
    return JPetGeomMapping::kBadSlotNumber;
  }
  auto index = layerNr - 1;
  if (fThetaToSlot.at(index).find(theta) == fThetaToSlot.at(index).end()) {
    ERROR("No slot number found for theta :" + std::to_string(theta) + " in layer:" + std::to_string(layerNr));
    return JPetGeomMapping::kBadSlotNumber;
  }
  return fThetaToSlot.at(index).at(theta);
}

const size_t JPetGeomMapping::getGlobalSlotNumber(const JPetBarrelSlot& slot) const
{
  auto layerNr = getLayerNumber(slot.getLayer());
  auto index = layerNr - 1;
  auto previousSlots = 0;
  /// We add all previously defined slots in previous layers
  for (auto i = 0u; i < fNumberOfSlotsInLayer.size(); i++) {
    if (index <= i) break; /// if we reach our layer
    previousSlots = previousSlots  + fNumberOfSlotsInLayer[i];
  }
  auto slotNrInLayer = getSlotNumber(slot);
  if (slotNrInLayer == JPetGeomMapping::kBadSlotNumber) {
    return JPetGeomMapping::kBadSlotNumber;
  } else {
    return previousSlots + getSlotNumber(slot);
  }
}

const StripPos JPetGeomMapping::getStripPos(const JPetBarrelSlot& slot)const
{
  return {.layer = getLayerNumber(slot.getLayer()), .slot = getSlotNumber(slot)};
}

const vector< size_t > JPetGeomMapping::getLayersSizes() const
{
  vector<size_t> res;
  for (size_t i = 1, n = getLayersCount(); i <= n; i++)
    res.push_back(getSlotsCount(i));
  return res;
}

const size_t JPetGeomMapping::calcDeltaID(const JPetBarrelSlot& slot1, const JPetBarrelSlot& slot2) const
{
  if (slot1.getLayer().getID() == slot2.getLayer().getID()) {
    int delta_ID = abs(getSlotNumber(slot1) - getSlotNumber(slot2));
    int layer_size = getSlotsCount(slot1.getLayer());
    int half_layer_size = layer_size / 2;
    if ( delta_ID > half_layer_size ) return layer_size - delta_ID;
    return delta_ID;
{'fraction_non_alphanumeric': 0.12218890554722639, 'fraction_numerical': 0.008808095952023988, 'mean_word_length': 4.558854166666666, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3933771', 'n_tokens_mistral': 3696, 'n_tokens_neox': 3626, 'n_words': 852}