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
#include "CondFormats/RunInfo/interface/FillInfo.h"
#include "CondFormats/Common/interface/TimeConversions.h"
#include <algorithm>
#include <iterator>
#include <stdexcept>

//helper function: returns the positions of the bits in the bitset that are set (i.e., have a value of 1).
static std::vector<unsigned short> bitsetToVector(std::bitset<FillInfo::bunchSlots + 1> const& bs) {
  std::vector<unsigned short> vec;
  //reserve space only for the bits in the bitset that are set
  vec.reserve(bs.count());
  for (size_t i = 0; i < bs.size(); ++i) {
    if (bs.test(i))
      vec.push_back((unsigned short)i);
  }
  return vec;
}

//helper function: returns the enum for fill types in string type
static std::string fillTypeToString(FillInfo::FillTypeId const& fillType) {
  std::string s_fillType("UNKNOWN");
  switch (fillType) {
    case FillInfo::UNKNOWN:
      s_fillType = std::string("UNKNOWN");
      break;
    case FillInfo::PROTONS:
      s_fillType = std::string("PROTONS");
      break;
    case FillInfo::IONS:
      s_fillType = std::string("IONS");
      break;
    case FillInfo::COSMICS:
      s_fillType = std::string("COSMICS");
      break;
    case FillInfo::GAP:
      s_fillType = std::string("GAP");
      break;
    default:
      s_fillType = std::string("UNKNOWN");
  }
  return s_fillType;
}

//helper function: returns the enum for particle types in string type
static std::string particleTypeToString(FillInfo::ParticleTypeId const& particleType) {
  std::string s_particleType("NONE");
  switch (particleType) {
    case FillInfo::NONE:
      s_particleType = std::string("NONE");
      break;
    case FillInfo::PROTON:
      s_particleType = std::string("PROTON");
      break;
    case FillInfo::PB82:
      s_particleType = std::string("PB82");
      break;
    case FillInfo::AR18:
      s_particleType = std::string("AR18");
      break;
    case FillInfo::D:
      s_particleType = std::string("D");
      break;
    case FillInfo::XE54:
      s_particleType = std::string("XE54");
      break;
    default:
      s_particleType = std::string("NONE");
  }
  return s_particleType;
}

FillInfo::FillInfo()
    : m_isData(false),
      m_lhcFill(0),
      m_bunches1(0),
      m_bunches2(0),
      m_collidingBunches(0),
      m_targetBunches(0),
      m_fillType(FillTypeId::UNKNOWN),
      m_particles1(ParticleTypeId::NONE),
      m_particles2(ParticleTypeId::NONE),
      m_crossingAngle(0.),
      m_betastar(0.),
      m_intensity1(0.),
      m_intensity2(0.),
      m_energy(0.),
      m_createTime(0),
      m_beginTime(0),
      m_endTime(0),
      m_injectionScheme("None") {}

FillInfo::FillInfo(unsigned short const& lhcFill, bool const& fromData)
    : m_isData(fromData),
      m_lhcFill(lhcFill),
      m_bunches1(0),
      m_bunches2(0),
      m_collidingBunches(0),
      m_targetBunches(0),
      m_fillType(FillTypeId::UNKNOWN),
      m_particles1(ParticleTypeId::NONE),
      m_particles2(ParticleTypeId::NONE),
      m_crossingAngle(0.),
      m_betastar(0.),
      m_intensity1(0.),
      m_intensity2(0.),
      m_energy(0.),
      m_createTime(0),
      m_beginTime(0),
      m_endTime(0),
      m_injectionScheme("None") {}

FillInfo::~FillInfo() {}

//reset instance
void FillInfo::setFill(unsigned short const& lhcFill, bool const& fromData) {
  m_isData = fromData;
  m_lhcFill = lhcFill;
  m_bunches1 = 0;
  m_bunches2 = 0;
  m_collidingBunches = 0;
  m_targetBunches = 0;
  m_fillType = FillTypeId::UNKNOWN;
  m_particles1 = ParticleTypeId::NONE;
  m_particles2 = ParticleTypeId::NONE;
  m_crossingAngle = 0.;
  m_betastar = 0.;
  m_intensity1 = 0;
  m_intensity2 = 0;
  m_energy = 0.;
  m_createTime = 0;
  m_beginTime = 0;
  m_endTime = 0;
  m_injectionScheme = "None";
  m_bunchConfiguration1.reset();
  m_bunchConfiguration2.reset();
}

//getters
unsigned short const FillInfo::fillNumber() const { return m_lhcFill; }

bool const FillInfo::isData() const { return m_isData; }

unsigned short const FillInfo::bunchesInBeam1() const { return m_bunches1; }

unsigned short const FillInfo::bunchesInBeam2() const { return m_bunches2; }

unsigned short const FillInfo::collidingBunches() const { return m_collidingBunches; }

unsigned short const FillInfo::targetBunches() const { return m_targetBunches; }

FillInfo::FillTypeId const FillInfo::fillType() const { return m_fillType; }

FillInfo::ParticleTypeId const FillInfo::particleTypeForBeam1() const { return m_particles1; }

FillInfo::ParticleTypeId const FillInfo::particleTypeForBeam2() const { return m_particles2; }

float const FillInfo::crossingAngle() const { return m_crossingAngle; }

float const FillInfo::betaStar() const { return m_betastar; }

float const FillInfo::intensityForBeam1() const { return m_intensity1; }

float const FillInfo::intensityForBeam2() const { return m_intensity2; }

float const FillInfo::energy() const { return m_energy; }

cond::Time_t const FillInfo::createTime() const { return m_createTime; }

cond::Time_t const FillInfo::beginTime() const { return m_beginTime; }

cond::Time_t const FillInfo::endTime() const { return m_endTime; }

std::string const& FillInfo::injectionScheme() const { return m_injectionScheme; }

//returns a boolean, true if the injection scheme has a leading 25ns
//TODO: parse the circulating bunch configuration, instead of the string.
bool FillInfo::is25nsBunchSpacing() const {
  const std::string prefix("25ns");
  return std::equal(prefix.begin(), prefix.end(), m_injectionScheme.begin());
}

//returns a boolean, true if the bunch slot number is in the circulating bunch configuration
bool FillInfo::isBunchInBeam1(size_t const& bunch) const {
  if (bunch == 0)
    throw std::out_of_range("0 not allowed");  //CMS starts counting bunch crossing from 1!
  return m_bunchConfiguration1.test(bunch);
}

bool FillInfo::isBunchInBeam2(size_t const& bunch) const {
  if (bunch == 0)
    throw std::out_of_range("0 not allowed");  //CMS starts counting bunch crossing from 1!
  return m_bunchConfiguration2.test(bunch);
}

//member functions returning *by value* a vector with all filled bunch slots
std::vector<unsigned short> FillInfo::bunchConfigurationForBeam1() const {
  return bitsetToVector(m_bunchConfiguration1);
}

std::vector<unsigned short> FillInfo::bunchConfigurationForBeam2() const {
  return bitsetToVector(m_bunchConfiguration2);
}

//setters
void FillInfo::setBunchesInBeam1(unsigned short const& bunches) { m_bunches1 = bunches; }

void FillInfo::setBunchesInBeam2(unsigned short const& bunches) { m_bunches2 = bunches; }

void FillInfo::setCollidingBunches(unsigned short const& collidingBunches) { m_collidingBunches = collidingBunches; }

void FillInfo::setTargetBunches(unsigned short const& targetBunches) { m_targetBunches = targetBunches; }

void FillInfo::setFillType(FillInfo::FillTypeId const& fillType) { m_fillType = fillType; }

void FillInfo::setParticleTypeForBeam1(FillInfo::ParticleTypeId const& particleType) { m_particles1 = particleType; }

void FillInfo::setParticleTypeForBeam2(FillInfo::ParticleTypeId const& particleType) { m_particles2 = particleType; }

void FillInfo::setCrossingAngle(float const& angle) { m_crossingAngle = angle; }

void FillInfo::setBetaStar(float const& betaStar) { m_betastar = betaStar; }

void FillInfo::setIntensityForBeam1(float const& intensity) { m_intensity1 = intensity; }

void FillInfo::setIntensityForBeam2(float const& intensity) { m_intensity2 = intensity; }

void FillInfo::setEnergy(float const& energy) { m_energy = energy; }

void FillInfo::setCreationTime(cond::Time_t const& createTime) { m_createTime = createTime; }

void FillInfo::setBeginTime(cond::Time_t const& beginTime) { m_beginTime = beginTime; }

void FillInfo::setEndTime(cond::Time_t const& endTime) { m_endTime = endTime; }

void FillInfo::setInjectionScheme(std::string const& injectionScheme) { m_injectionScheme = injectionScheme; }

//sets all values in one go
void FillInfo::setBeamInfo(unsigned short const& bunches1,
                           unsigned short const& bunches2,
                           unsigned short const& collidingBunches,
                           unsigned short const& targetBunches,
                           FillTypeId const& fillType,
                           ParticleTypeId const& particleType1,
                           ParticleTypeId const& particleType2,
                           float const& angle,
                           float const& beta,
                           float const& intensity1,
                           float const& intensity2,
                           float const& energy,
                           cond::Time_t const& createTime,
                           cond::Time_t const& beginTime,
                           cond::Time_t const& endTime,
                           std::string const& scheme,
                           std::bitset<bunchSlots + 1> const& bunchConf1,
                           std::bitset<bunchSlots + 1> const& bunchConf2) {
  this->setBunchesInBeam1(bunches1);
  this->setBunchesInBeam2(bunches2);
  this->setCollidingBunches(collidingBunches);
  this->setTargetBunches(targetBunches);
  this->setFillType(fillType);
  this->setParticleTypeForBeam1(particleType1);
  this->setParticleTypeForBeam2(particleType2);
  this->setCrossingAngle(angle);
  this->setBetaStar(beta);
  this->setIntensityForBeam1(intensity1);
  this->setIntensityForBeam2(intensity2);
  this->setEnergy(energy);
  this->setCreationTime(createTime);
  this->setBeginTime(beginTime);
  this->setEndTime(endTime);
  this->setInjectionScheme(scheme);
  this->setBunchBitsetForBeam1(bunchConf1);
  this->setBunchBitsetForBeam2(bunchConf2);
}

void FillInfo::print(std::stringstream& ss) const {
  ss << "LHC fill: " << m_lhcFill << std::endl
     << "Bunches in Beam 1: " << m_bunches1 << std::endl
     << "Bunches in Beam 2: " << m_bunches2 << std::endl
     << "Colliding bunches at IP5: " << m_collidingBunches << std::endl
     << "Target bunches at IP5: " << m_targetBunches << std::endl
     << "Fill type: " << fillTypeToString(m_fillType) << std::endl
     << "Particle type for Beam 1: " << particleTypeToString(m_particles1) << std::endl
     << "Particle type for Beam 2: " << particleTypeToString(m_particles2) << std::endl
     << "Crossing angle (urad): " << m_crossingAngle << std::endl
     << "Beta star (cm): " << m_betastar << std::endl
     << "Average Intensity for Beam 1 (number of charges): " << m_intensity1 << std::endl
     << "Average Intensity for Beam 2 (number of charges): " << m_intensity2 << std::endl
     << "Energy (GeV): " << m_energy << std::endl
     << "Creation time of the fill: " << boost::posix_time::to_iso_extended_string(cond::time::to_boost(m_createTime))
     << std::endl
     << "Begin time of Stable Beam flag: "
     << boost::posix_time::to_iso_extended_string(cond::time::to_boost(m_beginTime)) << std::endl
     << "End time of the fill: " << boost::posix_time::to_iso_extended_string(cond::time::to_boost(m_endTime))
     << std::endl
     << "Injection scheme as given by LPC: " << m_injectionScheme << std::endl;
  std::vector<unsigned short> bunchVector1 = this->bunchConfigurationForBeam1();
  std::vector<unsigned short> bunchVector2 = this->bunchConfigurationForBeam2();
  ss << "Bunches filled for Beam 1 (total " << bunchVector1.size() << "): ";
  std::copy(bunchVector1.begin(), bunchVector1.end(), std::ostream_iterator<unsigned short>(ss, ", "));
  ss << std::endl;
  ss << "Bunches filled for Beam 2 (total " << bunchVector2.size() << "): ";
  std::copy(bunchVector2.begin(), bunchVector2.end(), std::ostream_iterator<unsigned short>(ss, ", "));
  ss << std::endl;
}

//protected getters
std::bitset<FillInfo::bunchSlots + 1> const& FillInfo::bunchBitsetForBeam1() const { return m_bunchConfiguration1; }

std::bitset<FillInfo::bunchSlots + 1> const& FillInfo::bunchBitsetForBeam2() const { return m_bunchConfiguration2; }

//protected setters
void FillInfo::setBunchBitsetForBeam1(std::bitset<FillInfo::bunchSlots + 1> const& bunchConfiguration) {
  m_bunchConfiguration1 = bunchConfiguration;
}

void FillInfo::setBunchBitsetForBeam2(std::bitset<FillInfo::bunchSlots + 1> const& bunchConfiguration) {
  m_bunchConfiguration2 = bunchConfiguration;
}

std::ostream& operator<<(std::ostream& os, FillInfo fillInfo) {
  std::stringstream ss;
  fillInfo.print(ss);
  os << ss.str();
  return os;
}