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

#include "Alignment/CocoaToDDL/interface/CocoaUnitsTable.h"
#include <CLHEP/Units/SystemOfUnits.h>

#include <iomanip>
#include <cmath>  // include floating-point std::abs functions

CocoaUnitsTable CocoaUnitDefinition::theUnitsTable;

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

CocoaUnitDefinition::CocoaUnitDefinition(const ALIstring& name,
                                         const ALIstring& symbol,
                                         const ALIstring& category,
                                         ALIdouble value)
    : Name(name), SymbolName(symbol), Value(value) {
  //
  //does the Category objet already exist ?
  size_t nbCat = theUnitsTable.size();
  size_t i = 0;
  while ((i < nbCat) && (theUnitsTable[i]->GetName() != category))
    i++;
  if (i == nbCat)
    theUnitsTable.push_back(new CocoaUnitsCategory(category));
  CategoryIndex = i;
  //
  //insert this Unit in the Unitstable
  (theUnitsTable[CategoryIndex]->GetUnitsList()).push_back(this);

  //update string max length for name and symbol
  theUnitsTable[i]->UpdateNameMxLen((ALIint)name.length());
  theUnitsTable[i]->UpdateSymbMxLen((ALIint)symbol.length());
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

CocoaUnitDefinition::~CocoaUnitDefinition() {
  for (size_t i = 0; i < theUnitsTable.size(); i++) {
    delete theUnitsTable[i];
  }
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

CocoaUnitDefinition::CocoaUnitDefinition(const CocoaUnitDefinition& right) { *this = right; }

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

CocoaUnitDefinition& CocoaUnitDefinition::operator=(const CocoaUnitDefinition& right) {
  if (this != &right) {
    Name = right.Name;
    SymbolName = right.SymbolName;
    Value = right.Value;
    CategoryIndex = right.CategoryIndex;
  }
  return *this;
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

ALIint CocoaUnitDefinition::operator==(const CocoaUnitDefinition& right) const { return (this == &right); }

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

ALIint CocoaUnitDefinition::operator!=(const CocoaUnitDefinition& right) const { return (this != &right); }

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

CocoaUnitsTable& CocoaUnitDefinition::GetUnitsTable() { return theUnitsTable; }

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

ALIdouble CocoaUnitDefinition::GetValueOf(const ALIstring& str) {
  if (theUnitsTable.empty())
    BuildUnitsTable();
  ALIstring name, symbol;
  for (size_t i = 0; i < theUnitsTable.size(); i++) {
    CocoaUnitsContainer& units = theUnitsTable[i]->GetUnitsList();
    for (size_t j = 0; j < units.size(); j++) {
      name = units[j]->GetName();
      symbol = units[j]->GetSymbol();
      if (str == name || str == symbol)
        return units[j]->GetValue();
    }
  }
  std::cout << "Warning from CocoaUnitDefinition::GetValueOf(" << str << ")."
            << " The unit " << str << " does not exist in UnitsTable."
            << " Return Value = 0." << std::endl;
  return 0.;
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

ALIstring CocoaUnitDefinition::GetCategory(const ALIstring& str) {
  if (theUnitsTable.empty())
    BuildUnitsTable();
  ALIstring name, symbol;
  for (size_t i = 0; i < theUnitsTable.size(); i++) {
    CocoaUnitsContainer& units = theUnitsTable[i]->GetUnitsList();
    for (size_t j = 0; j < units.size(); j++) {
      name = units[j]->GetName();
      symbol = units[j]->GetSymbol();
      if (str == name || str == symbol)
        return theUnitsTable[i]->GetName();
    }
  }
  std::cout << "Warning from CocoaUnitDefinition::GetCategory(" << str << ")."
            << " The unit " << str << " does not exist in UnitsTable."
            << " Return category = None" << std::endl;
  name = "None";
  return name;
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

void CocoaUnitDefinition::PrintDefinition() {
  ALIint nameL = theUnitsTable[CategoryIndex]->GetNameMxLen();
  ALIint symbL = theUnitsTable[CategoryIndex]->GetSymbMxLen();
  std::cout << std::setw(nameL) << Name << " (" << std::setw(symbL) << SymbolName << ") = " << Value << std::endl;
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

void CocoaUnitDefinition::BuildUnitsTable() {
  using namespace CLHEP;
  //Length
  new CocoaUnitDefinition("meter", "m", "Length", meter);
  new CocoaUnitDefinition("centimeter", "cm", "Length", centimeter);
  new CocoaUnitDefinition("millimeter", "mm", "Length", millimeter);
  // new CocoaUnitDefinition("micrometer","um"      ,"Length",micrometer);
  new CocoaUnitDefinition("micrometer", "mum", "Length", micrometer);
  new CocoaUnitDefinition("nanometer", "nm", "Length", nanometer);
  new CocoaUnitDefinition("angstrom", "Ang", "Length", angstrom);
  new CocoaUnitDefinition("fermi", "fm", "Length", fermi);

  //Surface
  new CocoaUnitDefinition("kilometer2", "km2", "Surface", kilometer2);
  new CocoaUnitDefinition("meter2", "m2", "Surface", meter2);
  new CocoaUnitDefinition("centimeter2", "cm2", "Surface", centimeter2);
  new CocoaUnitDefinition("millimeter2", "mm2", "Surface", millimeter2);
  new CocoaUnitDefinition("barn", "barn", "Surface", barn);
  new CocoaUnitDefinition("millibarn", "mbarn", "Surface", millibarn);
  new CocoaUnitDefinition("microbarn", "mubarn", "Surface", microbarn);
  new CocoaUnitDefinition("nanobarn", "nbarn", "Surface", nanobarn);
  new CocoaUnitDefinition("picobarn", "pbarn", "Surface", picobarn);

  //Volume
  new CocoaUnitDefinition("kilometer3", "km3", "Volume", kilometer3);
  new CocoaUnitDefinition("meter3", "m3", "Volume", meter3);
  new CocoaUnitDefinition("centimeter3", "cm3", "Volume", centimeter3);
  new CocoaUnitDefinition("millimeter3", "mm3", "Volume", millimeter3);

  //Angle
  new CocoaUnitDefinition("radian", "rad", "Angle", radian);
  new CocoaUnitDefinition("milliradian", "mrad", "Angle", milliradian);
  new CocoaUnitDefinition("steradian", "sr", "Angle", steradian);
  new CocoaUnitDefinition("degree", "deg", "Angle", degree);

  //Time
  new CocoaUnitDefinition("second", "s", "Time", second);
  new CocoaUnitDefinition("millisecond", "ms", "Time", millisecond);
  new CocoaUnitDefinition("microsecond", "mus", "Time", microsecond);
  new CocoaUnitDefinition("nanosecond", "ns", "Time", nanosecond);
  new CocoaUnitDefinition("picosecond", "ps", "Time", picosecond);

  //Frequency
  new CocoaUnitDefinition("hertz", "Hz", "Frequency", hertz);
  new CocoaUnitDefinition("kilohertz", "kHz", "Frequency", kilohertz);
  new CocoaUnitDefinition("megahertz", "MHz", "Frequency", megahertz);

  //Electric charge
  new CocoaUnitDefinition("eplus", "e+", "Electric charge", eplus);
  new CocoaUnitDefinition("coulomb", "C", "Electric charge", coulomb);

  //Energy
  new CocoaUnitDefinition("electronvolt", "eV", "Energy", electronvolt);
  new CocoaUnitDefinition("kiloelectronvolt", "keV", "Energy", kiloelectronvolt);
  new CocoaUnitDefinition("megaelectronvolt", "MeV", "Energy", megaelectronvolt);
  new CocoaUnitDefinition("gigaelectronvolt", "GeV", "Energy", gigaelectronvolt);
  new CocoaUnitDefinition("teraelectronvolt", "TeV", "Energy", teraelectronvolt);
  new CocoaUnitDefinition("petaelectronvolt", "PeV", "Energy", petaelectronvolt);
  new CocoaUnitDefinition("joule", "J", "Energy", joule);

  //Mass
  new CocoaUnitDefinition("milligram", "mg", "Mass", milligram);
  new CocoaUnitDefinition("gram", "g", "Mass", gram);
  new CocoaUnitDefinition("kilogram", "kg", "Mass", kilogram);

  //Volumic Mass
  new CocoaUnitDefinition("g/cm3", "g/cm3", "Volumic Mass", g / cm3);
  new CocoaUnitDefinition("mg/cm3", "mg/cm3", "Volumic Mass", mg / cm3);
  new CocoaUnitDefinition("kg/m3", "kg/m3", "Volumic Mass", kg / m3);

  //Power
  new CocoaUnitDefinition("watt", "W", "Power", watt);

  //Force
  new CocoaUnitDefinition("newton", "N", "Force", newton);

  //Pressure
  new CocoaUnitDefinition("pascal", "Pa", "Pressure", pascal);
  new CocoaUnitDefinition("bar", "bar", "Pressure", bar);
  new CocoaUnitDefinition("atmosphere", "atm", "Pressure", atmosphere);

  //Electric current
  new CocoaUnitDefinition("ampere", "A", "Electric current", ampere);
  new CocoaUnitDefinition("milliampere", "mA", "Electric current", milliampere);
  new CocoaUnitDefinition("microampere", "muA", "Electric current", microampere);
  new CocoaUnitDefinition("nanoampere", "nA", "Electric current", nanoampere);

  //Electric potential
  new CocoaUnitDefinition("volt", "V", "Electric potential", volt);
  new CocoaUnitDefinition("kilovolt", "kV", "Electric potential", kilovolt);
  new CocoaUnitDefinition("megavolt", "MV", "Electric potential", megavolt);

  //Magnetic flux
  new CocoaUnitDefinition("weber", "Wb", "Magnetic flux", weber);

  //Magnetic flux density
  new CocoaUnitDefinition("tesla", "T", "Magnetic flux density", tesla);
  new CocoaUnitDefinition("kilogauss", "kG", "Magnetic flux density", kilogauss);
  new CocoaUnitDefinition("gauss", "G", "Magnetic flux density", gauss);

  //Temperature
  new CocoaUnitDefinition("kelvin", "K", "Temperature", kelvin);

  //Amount of substance
  new CocoaUnitDefinition("mole", "mol", "Amount of substance", mole);

  //Activity
  new CocoaUnitDefinition("becquerel", "Bq", "Activity", becquerel);
  new CocoaUnitDefinition("curie", "Ci", "Activity", curie);

  //Dose
  new CocoaUnitDefinition("gray", "Gy", "Dose", gray);
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

void CocoaUnitDefinition::PrintUnitsTable() {
  std::cout << "\n          ----- The Table of Units ----- \n";
  for (size_t i = 0; i < theUnitsTable.size(); i++) {
    theUnitsTable[i]->PrintCategory();
  }
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

CocoaUnitsCategory::CocoaUnitsCategory(const ALIstring& name) : Name(name), UnitsList(), NameMxLen(0), SymbMxLen(0) {}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

CocoaUnitsCategory::~CocoaUnitsCategory() {}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

CocoaUnitsCategory::CocoaUnitsCategory(const CocoaUnitsCategory& right) { *this = right; }

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

CocoaUnitsCategory& CocoaUnitsCategory::operator=(const CocoaUnitsCategory& right) {
  if (this != &right) {
    Name = right.Name;
    UnitsList = right.UnitsList;
    NameMxLen = right.NameMxLen;
    SymbMxLen = right.SymbMxLen;
  }
  return *this;
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

ALIint CocoaUnitsCategory::operator==(const CocoaUnitsCategory& right) const { return (this == &right); }

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

ALIint CocoaUnitsCategory::operator!=(const CocoaUnitsCategory& right) const { return (this != &right); }

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

void CocoaUnitsCategory::PrintCategory() {
  std::cout << "\n  category: " << Name << std::endl;
  for (size_t i = 0; i < UnitsList.size(); i++)
    UnitsList[i]->PrintDefinition();
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

CocoaBestUnit::CocoaBestUnit(ALIdouble value, const ALIstring& category) {
  // find the category
  CocoaUnitsTable& theUnitsTable = CocoaUnitDefinition::GetUnitsTable();
  if (theUnitsTable.empty())
    CocoaUnitDefinition::BuildUnitsTable();  //t should be done somewhere else
  size_t nbCat = theUnitsTable.size();
  size_t i = 0;
  while ((i < nbCat) && (theUnitsTable[i]->GetName() != category))
    i++;
  if (i == nbCat) {
    std::cout << " CocoaBestUnit: the category " << category << " does not exist !!" << nbCat << std::endl;
    std::exception();  //"Missing unit category !");
  }
  //
  IndexOfCategory = i;
  nbOfVals = 1;
  Value[0] = value;
  Value[1] = 0.;
  Value[2] = 0.;

  //COCOA internal units are in meters, not mm as in CLHEP
  if (category == "Length") {
    Value[0] *= 1000.;
    Value[1] *= 1000.;
    Value[2] *= 1000.;
  }
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

CocoaBestUnit::CocoaBestUnit(const CLHEP::Hep3Vector& value, const ALIstring& category) {
  // find the category
  CocoaUnitsTable& theUnitsTable = CocoaUnitDefinition::GetUnitsTable();
  size_t nbCat = theUnitsTable.size();
  size_t i = 0;
  while ((i < nbCat) && (theUnitsTable[i]->GetName() != category))
    i++;
  if (i == nbCat) {
    std::cerr << " CocoaBestUnit: the category " << category << " does not exist." << std::endl;
    std::exception();  //"Unit category not existing !");
  }
  //
  IndexOfCategory = i;
  nbOfVals = 3;
  Value[0] = value.x();
  Value[1] = value.y();
  Value[2] = value.z();
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

CocoaBestUnit::~CocoaBestUnit() {}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

std::ostream& operator<<(std::ostream& flux, CocoaBestUnit a) {
  CocoaUnitsTable& theUnitsTable = CocoaUnitDefinition::GetUnitsTable();
  CocoaUnitsContainer& List = theUnitsTable[a.IndexOfCategory]->GetUnitsList();
  ALIint len = theUnitsTable[a.IndexOfCategory]->GetSymbMxLen();

  ALIint ksup(-1), kinf(-1);
  ALIdouble umax(0.), umin(ALI_DBL_MAX);
  ALIdouble rsup(ALI_DBL_MAX), rinf(0.);

  //for a ThreeVector, choose the best unit for the biggest value
  ALIdouble value = std::max(std::max(std::abs(a.Value[0]), std::abs(a.Value[1])), std::abs(a.Value[2]));

  for (size_t k = 0; k < List.size(); k++) {
    ALIdouble unit = List[k]->GetValue();
    if (value == ALI_DBL_MAX) {
      if (unit > umax) {
        umax = unit;
        ksup = k;
      }
    } else if (value <= ALI_DBL_MIN) {
      if (unit < umin) {
        umin = unit;
        kinf = k;
      }
    }

    else {
      ALIdouble ratio = value / unit;
      if ((ratio >= 1.) && (ratio < rsup)) {
        rsup = ratio;
        ksup = k;
      }
      if ((ratio < 1.) && (ratio > rinf)) {
        rinf = ratio;
        kinf = k;
      }
    }
  }

  ALIint index = ksup;
  if (index == -1)
    index = kinf;
  if (index == -1)
    index = 0;

  for (ALIint j = 0; j < a.nbOfVals; j++) {
    flux << a.Value[j] / (List[index]->GetValue()) << " ";
  }

  std::ios::fmtflags oldform = flux.flags();

  flux.setf(std::ios::left, std::ios::adjustfield);
  flux << std::setw(len) << List[index]->GetSymbol();
  flux.flags(oldform);

  return flux;
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....