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
// -*- C++ -*-
//
// Package:     HcalOnlineDb
// Class  :     HcalAssistant
//
// Implementation:
//     <Notes on implementation>
//
// Original Author:  Gena Kukartsev, kukarzev@fnal.gov
//         Created:  Thu Jul 16 11:39:22 CEST 2009
//

#include <ctime>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include "CaloOnlineTools/HcalOnlineDb/interface/HcalAssistant.h"
#include "CaloOnlineTools/HcalOnlineDb/interface/ConnectionManager.h"
#include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationDatabaseException.hh"
#include "OnlineDB/Oracle/interface/Oracle.h"
#include "DataFormats/HcalDetId/interface/HcalDetId.h"

#ifdef HAVE_XDAQ
#include <toolbox/string.h>
#else
#include "CaloOnlineTools/HcalOnlineDb/interface/xdaq_compat.h"  // Includes typedef for log4cplus::Logger
#endif

using namespace std;
using namespace oracle::occi;

HcalAssistant::HcalAssistant() {
  addQuotes();
  srand(time(nullptr));
  listIsRead = false;
}

HcalAssistant::~HcalAssistant() {}

int HcalAssistant::addQuotes() {
  quotes.push_back("Fear is the path to the Dark Side...");
  quotes.push_back("You don't know the power of the Dark Side...");
  quotes.push_back("You must learn the ways of the Force!");
  quotes.push_back("Where's the money, Lebowski?!");
  quotes.push_back("You see what happens when you find a stranger in the Alps!!!?");
  quotes.push_back(
      "You hear this? This is the sound of inevitability. This is the sound of your death. Goodbye, mr. Anderson");
  quotes.push_back("Welcome to the desert of the Real");
  quotes.push_back("In Tyler we trust");
  quotes.push_back("How about a little snack?..Let's have a snack now, we can get friendly later");
  quotes.push_back("Is he human? Hey, no need for name calling!");
  quotes.push_back("Frankly, my dear, I don't give a damn");
  quotes.push_back("I've a feeling we're not in Kansas anymore");
  quotes.push_back("What we've got here is failure to communicate");
  quotes.push_back("I love the smell of napalm in the morning!");
  quotes.push_back("I see stupid people");
  quotes.push_back("Stella! Hey, Stella!");
  quotes.push_back("Houston, we have a problem");
  quotes.push_back("Mrs. Robinson, you're trying to seduce me. Aren't you?");
  quotes.push_back("I feel the need - the need for speed!");
  quotes.push_back("He's got emotional problems. What, beyond pacifism?");
  return quotes.size();
}

std::string HcalAssistant::getRandomQuote() {
  int _quotes_array_size = quotes.size();
  int _num = rand() % _quotes_array_size;
  return quotes[_num];
}

std::string HcalAssistant::getUserName(void) {
  struct passwd* _pwd = getpwuid(geteuid());
  std::string _name(_pwd->pw_name);
  return _name;
}

int HcalAssistant::getGeomId(HcalSubdetector _det, int _ieta, int _iphi, int _depth) {
  int _geomId = _det + 10 * _depth + 100 * _iphi + 10000 * abs(_ieta);
  if (_ieta < 0) {
    _geomId = -_geomId;
  }
  return _geomId;
}

int HcalAssistant::getHcalIeta(int _geomId) {
  int _ieta = _geomId / 10000;
  return _ieta;
}

int HcalAssistant::getHcalIphi(int _geomId) {
  int _iphi = abs(_geomId % 10000) / 100;
  return _iphi;
}

int HcalAssistant::getHcalDepth(int _geomId) {
  int _depth = abs(_geomId % 100) / 10;
  return _depth;
}

HcalSubdetector HcalAssistant::getHcalSubdetector(int _geomId) {
  int _det = abs(_geomId) % 10;
  if (_det == 1)
    return HcalBarrel;
  else if (_det == 2)
    return HcalEndcap;
  else if (_det == 3)
    return HcalOuter;
  else if (_det == 4)
    return HcalForward;
  else if (_det == 5)
    return HcalTriggerTower;
  else if (_det == 0)
    return HcalEmpty;
  else
    return HcalOther;
}

std::string HcalAssistant::getSubdetectorString(int _geomId) {
  int _det = abs(_geomId) % 10;
  std::string s_det = "";
  if (_det == 1)
    s_det = "HB";
  else if (_det == 2)
    s_det = "HE";
  else if (_det == 3)
    s_det = "HO";
  else if (_det == 4)
    s_det = "HF";
  else if (_det == 5)
    s_det = "HT";
  else if (_det == 6)
    s_det = "ZDC";
  else if (_det == 0)
    s_det = "Empty";
  else
    s_det = "Other";
  return s_det;
}

HcalSubdetector HcalAssistant::getSubdetector(std::string _det) {
  if (_det.find("HB") != std::string::npos)
    return HcalBarrel;
  else if (_det.find("HE") != std::string::npos)
    return HcalEndcap;
  else if (_det.find("HF") != std::string::npos)
    return HcalForward;
  else if (_det.find("HO") != std::string::npos)
    return HcalOuter;
  else if (_det.find("HT") != std::string::npos)
    return HcalTriggerTower;
  else
    return HcalOther;
}

std::string HcalAssistant::getSubdetectorString(HcalSubdetector _det) {
  std::string sDet;
  if (_det == HcalBarrel)
    sDet = "HB";
  else if (_det == HcalEndcap)
    sDet = "HE";
  else if (_det == HcalForward)
    sDet = "HF";
  else if (_det == HcalOuter)
    sDet = "HO";
  else if (_det == HcalTriggerTower)
    sDet = "HT";
  else
    sDet = "other";
  return sDet;
}

std::string HcalAssistant::getZDCSectionString(HcalZDCDetId::Section _section) {
  std::string zdcSection;
  if (_section == HcalZDCDetId::EM)
    zdcSection = "ZDC EM";
  else if (_section == HcalZDCDetId::HAD)
    zdcSection = "ZDC HAD";
  else if (_section == HcalZDCDetId::LUM)
    zdcSection = "ZDC LUM";
  else if (_section == HcalZDCDetId::RPD)
    zdcSection = "ZDC RPD";
  else
    zdcSection = "UNKNOWN";
  return zdcSection;
}

HcalZDCDetId::Section HcalAssistant::getZDCSection(std::string _section) {
  if (_section.find("ZDC EM") != std::string::npos)
    return HcalZDCDetId::EM;
  else if (_section.find("ZDC HAD") != std::string::npos)
    return HcalZDCDetId::HAD;
  else if (_section.find("ZDC LUM") != std::string::npos)
    return HcalZDCDetId::LUM;
  else if (_section.find("ZDC RPD") != std::string::npos)
    return HcalZDCDetId::RPD;
  else
    return HcalZDCDetId::Unknown;
}

int HcalAssistant::getListOfChannelsFromDb() {
  int _n_channels = 0;
  static ConnectionManager conn;
  conn.connect();
  std::string query = "select ";
  query += "       channel_map_id,subdet,ieta,iphi,depth ";
  query += "from ";
  query += "       cms_hcl_hcal_cond.hcal_channels ";
  query += "where ";
  query += "       subdet='HB' or subdet='HE' or subdet='HF' or subdet='HO' ";
  try {
    oracle::occi::Statement* stmt = conn.getStatement(query);
    oracle::occi::ResultSet* rs = stmt->executeQuery();
    geom_to_rawid.clear();
    rawid_to_geom.clear();
    while (rs->next()) {
      _n_channels++;
      int _rawid = rs->getInt(1);
      int _geomId = getGeomId(getSubdetector(rs->getString(2)), rs->getInt(3), rs->getInt(4), rs->getInt(5));
      geom_to_rawid.insert(std::pair<int, int>(_geomId, _rawid));
      rawid_to_geom.insert(std::pair<int, int>(_rawid, _geomId));
    }
    listIsRead = true;
  } catch (SQLException& e) {
    std::cerr << ::toolbox::toString("Oracle  exception : %s", e.getMessage().c_str()) << std::endl;
    XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,
                ::toolbox::toString("Oracle  exception : %s", e.getMessage().c_str()));
  }
  conn.disconnect();
  return _n_channels;
}

int HcalAssistant::getGeomId(int _rawid) {
  if (listIsRead) {
    std::map<int, int>::const_iterator _geomId = rawid_to_geom.find(_rawid);
    if (_geomId != rawid_to_geom.end()) {
      return _geomId->second;
    } else
      return -1;
  } else
    return -1;
}

int HcalAssistant::getRawId(int _geomId) {
  if (listIsRead) {
    std::map<int, int>::const_iterator _rawid = geom_to_rawid.find(_geomId);
    if (_rawid != geom_to_rawid.end()) {
      return _rawid->second;
    } else
      return -1;
  } else
    return -1;
}

int HcalAssistant::getRawIdFromCmssw(int _geomId) {
  std::string s_det = getSubdetectorString(_geomId);
  if (s_det.find("HB") != std::string::npos || s_det.find("HE") != std::string::npos ||
      s_det.find("HF") != std::string::npos || s_det.find("HO") != std::string::npos ||
      s_det.find("HT") != std::string::npos) {
    HcalDetId _id(getSubdetector(s_det), getHcalIeta(_geomId), getHcalIphi(_geomId), getHcalDepth(_geomId));
    return _id.rawId();
  } else if (s_det.find("ZDC") != std::string::npos) {
    // FIXME: implement for ZDC channels
    return -1;
  } else {
    return -1;
  }
}

int HcalAssistant::getSubdetector(int _rawid) { return getHcalSubdetector(getGeomId(_rawid)); }

int HcalAssistant::getIeta(int _rawid) { return getHcalIeta(getGeomId(_rawid)); }

int HcalAssistant::getIphi(int _rawid) { return getHcalIphi(getGeomId(_rawid)); }

int HcalAssistant::getDepth(int _rawid) { return getHcalDepth(getGeomId(_rawid)); }

int HcalAssistant::getRawId(HcalSubdetector _det, int _ieta, int _iphi, int _depth) {
  return getRawId(getGeomId(_det, _ieta, _iphi, _depth));
}

int HcalAssistant::a_to_i(char* inbuf) {
  int result;
  sscanf(inbuf, "%d", &result);
  return result;
}