Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:00:07

0001 // -*- C++ -*-
0002 //
0003 // Package:     HcalOnlineDb
0004 // Class  :     HcalAssistant
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Gena Kukartsev, kukarzev@fnal.gov
0010 //         Created:  Thu Jul 16 11:39:22 CEST 2009
0011 //
0012 
0013 #include <ctime>
0014 #include <unistd.h>
0015 #include <sys/types.h>
0016 #include <pwd.h>
0017 #include "CaloOnlineTools/HcalOnlineDb/interface/HcalAssistant.h"
0018 #include "CaloOnlineTools/HcalOnlineDb/interface/ConnectionManager.h"
0019 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationDatabaseException.hh"
0020 #include "OnlineDB/Oracle/interface/Oracle.h"
0021 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
0022 
0023 #ifdef HAVE_XDAQ
0024 #include <toolbox/string.h>
0025 #else
0026 #include "CaloOnlineTools/HcalOnlineDb/interface/xdaq_compat.h"  // Includes typedef for log4cplus::Logger
0027 #endif
0028 
0029 using namespace std;
0030 using namespace oracle::occi;
0031 
0032 HcalAssistant::HcalAssistant() {
0033   addQuotes();
0034   srand(time(nullptr));
0035   listIsRead = false;
0036 }
0037 
0038 HcalAssistant::~HcalAssistant() {}
0039 
0040 int HcalAssistant::addQuotes() {
0041   quotes.push_back("Fear is the path to the Dark Side...");
0042   quotes.push_back("You don't know the power of the Dark Side...");
0043   quotes.push_back("You must learn the ways of the Force!");
0044   quotes.push_back("Where's the money, Lebowski?!");
0045   quotes.push_back("You see what happens when you find a stranger in the Alps!!!?");
0046   quotes.push_back(
0047       "You hear this? This is the sound of inevitability. This is the sound of your death. Goodbye, mr. Anderson");
0048   quotes.push_back("Welcome to the desert of the Real");
0049   quotes.push_back("In Tyler we trust");
0050   quotes.push_back("How about a little snack?..Let's have a snack now, we can get friendly later");
0051   quotes.push_back("Is he human? Hey, no need for name calling!");
0052   quotes.push_back("Frankly, my dear, I don't give a damn");
0053   quotes.push_back("I've a feeling we're not in Kansas anymore");
0054   quotes.push_back("What we've got here is failure to communicate");
0055   quotes.push_back("I love the smell of napalm in the morning!");
0056   quotes.push_back("I see stupid people");
0057   quotes.push_back("Stella! Hey, Stella!");
0058   quotes.push_back("Houston, we have a problem");
0059   quotes.push_back("Mrs. Robinson, you're trying to seduce me. Aren't you?");
0060   quotes.push_back("I feel the need - the need for speed!");
0061   quotes.push_back("He's got emotional problems. What, beyond pacifism?");
0062   return quotes.size();
0063 }
0064 
0065 std::string HcalAssistant::getRandomQuote() {
0066   int _quotes_array_size = quotes.size();
0067   int _num = rand() % _quotes_array_size;
0068   return quotes[_num];
0069 }
0070 
0071 std::string HcalAssistant::getUserName(void) {
0072   struct passwd* _pwd = getpwuid(geteuid());
0073   std::string _name(_pwd->pw_name);
0074   return _name;
0075 }
0076 
0077 int HcalAssistant::getGeomId(HcalSubdetector _det, int _ieta, int _iphi, int _depth) {
0078   int _geomId = _det + 10 * _depth + 100 * _iphi + 10000 * abs(_ieta);
0079   if (_ieta < 0) {
0080     _geomId = -_geomId;
0081   }
0082   return _geomId;
0083 }
0084 
0085 int HcalAssistant::getHcalIeta(int _geomId) {
0086   int _ieta = _geomId / 10000;
0087   return _ieta;
0088 }
0089 
0090 int HcalAssistant::getHcalIphi(int _geomId) {
0091   int _iphi = abs(_geomId % 10000) / 100;
0092   return _iphi;
0093 }
0094 
0095 int HcalAssistant::getHcalDepth(int _geomId) {
0096   int _depth = abs(_geomId % 100) / 10;
0097   return _depth;
0098 }
0099 
0100 HcalSubdetector HcalAssistant::getHcalSubdetector(int _geomId) {
0101   int _det = abs(_geomId) % 10;
0102   if (_det == 1)
0103     return HcalBarrel;
0104   else if (_det == 2)
0105     return HcalEndcap;
0106   else if (_det == 3)
0107     return HcalOuter;
0108   else if (_det == 4)
0109     return HcalForward;
0110   else if (_det == 5)
0111     return HcalTriggerTower;
0112   else if (_det == 0)
0113     return HcalEmpty;
0114   else
0115     return HcalOther;
0116 }
0117 
0118 std::string HcalAssistant::getSubdetectorString(int _geomId) {
0119   int _det = abs(_geomId) % 10;
0120   std::string s_det = "";
0121   if (_det == 1)
0122     s_det = "HB";
0123   else if (_det == 2)
0124     s_det = "HE";
0125   else if (_det == 3)
0126     s_det = "HO";
0127   else if (_det == 4)
0128     s_det = "HF";
0129   else if (_det == 5)
0130     s_det = "HT";
0131   else if (_det == 6)
0132     s_det = "ZDC";
0133   else if (_det == 0)
0134     s_det = "Empty";
0135   else
0136     s_det = "Other";
0137   return s_det;
0138 }
0139 
0140 HcalSubdetector HcalAssistant::getSubdetector(std::string _det) {
0141   if (_det.find("HB") != std::string::npos)
0142     return HcalBarrel;
0143   else if (_det.find("HE") != std::string::npos)
0144     return HcalEndcap;
0145   else if (_det.find("HF") != std::string::npos)
0146     return HcalForward;
0147   else if (_det.find("HO") != std::string::npos)
0148     return HcalOuter;
0149   else if (_det.find("HT") != std::string::npos)
0150     return HcalTriggerTower;
0151   else
0152     return HcalOther;
0153 }
0154 
0155 std::string HcalAssistant::getSubdetectorString(HcalSubdetector _det) {
0156   std::string sDet;
0157   if (_det == HcalBarrel)
0158     sDet = "HB";
0159   else if (_det == HcalEndcap)
0160     sDet = "HE";
0161   else if (_det == HcalForward)
0162     sDet = "HF";
0163   else if (_det == HcalOuter)
0164     sDet = "HO";
0165   else if (_det == HcalTriggerTower)
0166     sDet = "HT";
0167   else
0168     sDet = "other";
0169   return sDet;
0170 }
0171 
0172 std::string HcalAssistant::getZDCSectionString(HcalZDCDetId::Section _section) {
0173   std::string zdcSection;
0174   if (_section == HcalZDCDetId::EM)
0175     zdcSection = "ZDC EM";
0176   else if (_section == HcalZDCDetId::HAD)
0177     zdcSection = "ZDC HAD";
0178   else if (_section == HcalZDCDetId::LUM)
0179     zdcSection = "ZDC LUM";
0180   else if (_section == HcalZDCDetId::RPD)
0181     zdcSection = "ZDC RPD";
0182   else
0183     zdcSection = "UNKNOWN";
0184   return zdcSection;
0185 }
0186 
0187 HcalZDCDetId::Section HcalAssistant::getZDCSection(std::string _section) {
0188   if (_section.find("ZDC EM") != std::string::npos)
0189     return HcalZDCDetId::EM;
0190   else if (_section.find("ZDC HAD") != std::string::npos)
0191     return HcalZDCDetId::HAD;
0192   else if (_section.find("ZDC LUM") != std::string::npos)
0193     return HcalZDCDetId::LUM;
0194   else if (_section.find("ZDC RPD") != std::string::npos)
0195     return HcalZDCDetId::RPD;
0196   else
0197     return HcalZDCDetId::Unknown;
0198 }
0199 
0200 int HcalAssistant::getListOfChannelsFromDb() {
0201   int _n_channels = 0;
0202   static ConnectionManager conn;
0203   conn.connect();
0204   std::string query = "select ";
0205   query += "       channel_map_id,subdet,ieta,iphi,depth ";
0206   query += "from ";
0207   query += "       cms_hcl_hcal_cond.hcal_channels ";
0208   query += "where ";
0209   query += "       subdet='HB' or subdet='HE' or subdet='HF' or subdet='HO' ";
0210   try {
0211     oracle::occi::Statement* stmt = conn.getStatement(query);
0212     oracle::occi::ResultSet* rs = stmt->executeQuery();
0213     geom_to_rawid.clear();
0214     rawid_to_geom.clear();
0215     while (rs->next()) {
0216       _n_channels++;
0217       int _rawid = rs->getInt(1);
0218       int _geomId = getGeomId(getSubdetector(rs->getString(2)), rs->getInt(3), rs->getInt(4), rs->getInt(5));
0219       geom_to_rawid.insert(std::pair<int, int>(_geomId, _rawid));
0220       rawid_to_geom.insert(std::pair<int, int>(_rawid, _geomId));
0221     }
0222     listIsRead = true;
0223   } catch (SQLException& e) {
0224     std::cerr << ::toolbox::toString("Oracle  exception : %s", e.getMessage().c_str()) << std::endl;
0225     XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,
0226                 ::toolbox::toString("Oracle  exception : %s", e.getMessage().c_str()));
0227   }
0228   conn.disconnect();
0229   return _n_channels;
0230 }
0231 
0232 int HcalAssistant::getGeomId(int _rawid) {
0233   if (listIsRead) {
0234     std::map<int, int>::const_iterator _geomId = rawid_to_geom.find(_rawid);
0235     if (_geomId != rawid_to_geom.end()) {
0236       return _geomId->second;
0237     } else
0238       return -1;
0239   } else
0240     return -1;
0241 }
0242 
0243 int HcalAssistant::getRawId(int _geomId) {
0244   if (listIsRead) {
0245     std::map<int, int>::const_iterator _rawid = geom_to_rawid.find(_geomId);
0246     if (_rawid != geom_to_rawid.end()) {
0247       return _rawid->second;
0248     } else
0249       return -1;
0250   } else
0251     return -1;
0252 }
0253 
0254 int HcalAssistant::getRawIdFromCmssw(int _geomId) {
0255   std::string s_det = getSubdetectorString(_geomId);
0256   if (s_det.find("HB") != std::string::npos || s_det.find("HE") != std::string::npos ||
0257       s_det.find("HF") != std::string::npos || s_det.find("HO") != std::string::npos ||
0258       s_det.find("HT") != std::string::npos) {
0259     HcalDetId _id(getSubdetector(s_det), getHcalIeta(_geomId), getHcalIphi(_geomId), getHcalDepth(_geomId));
0260     return _id.rawId();
0261   } else if (s_det.find("ZDC") != std::string::npos) {
0262     // FIXME: implement for ZDC channels
0263     return -1;
0264   } else {
0265     return -1;
0266   }
0267 }
0268 
0269 int HcalAssistant::getSubdetector(int _rawid) { return getHcalSubdetector(getGeomId(_rawid)); }
0270 
0271 int HcalAssistant::getIeta(int _rawid) { return getHcalIeta(getGeomId(_rawid)); }
0272 
0273 int HcalAssistant::getIphi(int _rawid) { return getHcalIphi(getGeomId(_rawid)); }
0274 
0275 int HcalAssistant::getDepth(int _rawid) { return getHcalDepth(getGeomId(_rawid)); }
0276 
0277 int HcalAssistant::getRawId(HcalSubdetector _det, int _ieta, int _iphi, int _depth) {
0278   return getRawId(getGeomId(_det, _ieta, _iphi, _depth));
0279 }
0280 
0281 int HcalAssistant::a_to_i(char* inbuf) {
0282   int result;
0283   sscanf(inbuf, "%d", &result);
0284   return result;
0285 }