CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/CaloOnlineTools/HcalOnlineDb/src/HcalAssistant.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     HcalOnlineDb
00004 // Class  :     HcalAssistant
00005 // 
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  Gena Kukartsev, kukarzev@fnal.gov
00010 //         Created:  Thu Jul 16 11:39:22 CEST 2009
00011 // $Id: HcalAssistant.cc,v 1.9 2010/08/06 20:24:11 wmtan Exp $
00012 //
00013 
00014 
00015 #include <ctime>
00016 #include <unistd.h>
00017 #include <sys/types.h>
00018 #include <pwd.h>
00019 #include "CaloOnlineTools/HcalOnlineDb/interface/HcalAssistant.h"
00020 #include "CaloOnlineTools/HcalOnlineDb/interface/ConnectionManager.h"
00021 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationDatabaseException.hh"
00022 #include "xgi/Utils.h"
00023 #include "toolbox/string.h"
00024 #include "OnlineDB/Oracle/interface/Oracle.h"
00025 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00026 
00027 using namespace std;
00028 using namespace oracle::occi;
00029 
00030 HcalAssistant::HcalAssistant()
00031 {
00032   addQuotes();
00033   srand(time(0));
00034   listIsRead = false;
00035 }
00036 
00037 
00038 HcalAssistant::~HcalAssistant()
00039 {
00040 }
00041 
00042 
00043 int HcalAssistant::addQuotes(){
00044   quotes.push_back("Fear is the path to the Dark Side...");
00045   quotes.push_back("You don't know the power of the Dark Side...");
00046   quotes.push_back("You must learn the ways of the Force!");
00047   quotes.push_back("Where's the money, Lebowski?!");
00048   quotes.push_back("You see what happens when you find a stranger in the Alps!!!?");
00049   quotes.push_back("You hear this? This is the sound of inevitability. This is the sound of your death. Goodbye, mr. Anderson");
00050   quotes.push_back("Welcome to the desert of the Real");
00051   quotes.push_back("In Tyler we trust");
00052   quotes.push_back("How about a little snack?..Let's have a snack now, we can get friendly later");
00053   quotes.push_back("Is he human? Hey, no need for name calling!");
00054   quotes.push_back("Frankly, my dear, I don't give a damn");
00055   quotes.push_back("I've a feeling we're not in Kansas anymore");
00056   quotes.push_back("What we've got here is failure to communicate");
00057   quotes.push_back("I love the smell of napalm in the morning!");
00058   quotes.push_back("I see stupid people");
00059   quotes.push_back("Stella! Hey, Stella!");
00060   quotes.push_back("Houston, we have a problem");
00061   quotes.push_back("Mrs. Robinson, you're trying to seduce me. Aren't you?");
00062   quotes.push_back("I feel the need - the need for speed!");
00063   quotes.push_back("He's got emotional problems. What, beyond pacifism?");
00064   return quotes.size();
00065 }
00066 
00067 
00068 
00069 std::string HcalAssistant::getRandomQuote(){
00070   int _quotes_array_size = quotes.size();
00071   int _num = rand()%_quotes_array_size;
00072   return quotes[_num];
00073 }
00074 
00075 
00076 std::string HcalAssistant::getUserName(void){
00077   struct passwd * _pwd = getpwuid(geteuid());
00078   std::string _name(_pwd->pw_name);
00079   return _name;
00080 }
00081 
00082 
00083 
00084 int HcalAssistant::getGeomId(HcalSubdetector _det, int _ieta, int _iphi, int _depth){
00085   int _geomId = 
00086     _det
00087     +   10 * _depth
00088     +  100 * _iphi
00089     +10000 * abs(_ieta);
00090   if (_ieta<0){
00091     _geomId = -_geomId;
00092   }
00093   return _geomId;
00094 }
00095 
00096 
00097 int HcalAssistant::getHcalIeta(int _geomId){
00098   int _ieta = _geomId/10000;
00099   return _ieta;
00100 }
00101 
00102 
00103 int HcalAssistant::getHcalIphi(int _geomId){
00104   int _iphi = abs( _geomId % 10000 )/100;
00105   return _iphi;
00106 }
00107 
00108 
00109 int HcalAssistant::getHcalDepth(int _geomId){
00110   int _depth = abs( _geomId % 100 ) / 10;
00111   return _depth;
00112 }
00113 
00114 
00115 HcalSubdetector HcalAssistant::getHcalSubdetector(int _geomId){
00116   int _det = abs(_geomId) % 10;
00117   if (_det==1) return HcalBarrel;
00118   else if (_det==2) return HcalEndcap;
00119   else if (_det==3) return HcalOuter;
00120   else if (_det==4) return HcalForward;
00121   else if (_det==5) return HcalTriggerTower;
00122   else if (_det==0) return HcalEmpty;
00123   else return HcalOther;
00124 }
00125 
00126 
00127 std::string HcalAssistant::getSubdetectorString(int _geomId){
00128   int _det = abs(_geomId) % 10;
00129   std::string s_det = "";
00130   if (_det==1) s_det = "HB";
00131   else if (_det==2) s_det = "HE";
00132   else if (_det==3) s_det = "HO";
00133   else if (_det==4) s_det = "HF";
00134   else if (_det==5) s_det = "HT";
00135   else if (_det==6) s_det = "ZDC";
00136   else if (_det==0) s_det = "Empty";
00137   else s_det = "Other";
00138   return s_det;
00139 }
00140 
00141 
00142 HcalSubdetector HcalAssistant::getSubdetector(std::string _det){
00143   if      ( _det.find("HB") != std::string::npos ) return HcalBarrel;
00144   else if ( _det.find("HE") != std::string::npos ) return HcalEndcap;
00145   else if ( _det.find("HF") != std::string::npos ) return HcalForward;
00146   else if ( _det.find("HO") != std::string::npos ) return HcalOuter;
00147   else if ( _det.find("HT") != std::string::npos ) return HcalTriggerTower;
00148   else return HcalOther;
00149 }
00150 
00151 std::string HcalAssistant::getSubdetectorString(HcalSubdetector _det){
00152   std::string sDet;
00153   if      ( _det==HcalBarrel)  sDet = "HB";
00154   else if ( _det==HcalEndcap)  sDet = "HE";
00155   else if      ( _det==HcalForward) sDet = "HF";
00156   else if      ( _det==HcalOuter)   sDet = "HO";
00157   else if      ( _det==HcalTriggerTower)   sDet = "HT";
00158   else sDet = "other";
00159   return sDet;
00160 }
00161 
00162 
00163 std::string HcalAssistant::getZDCSectionString(HcalZDCDetId::Section _section){
00164   std::string zdcSection;
00165   if           ( _section==HcalZDCDetId::EM)   zdcSection = "ZDC EM";
00166   else if      ( _section==HcalZDCDetId::HAD)  zdcSection = "ZDC HAD";
00167   else if      ( _section==HcalZDCDetId::LUM)  zdcSection = "ZDC LUM";
00168   else zdcSection = "UNKNOWN";
00169   return zdcSection;
00170 }
00171 
00172 
00173 HcalZDCDetId::Section HcalAssistant::getZDCSection(std::string _section){
00174   if      ( _section.find("ZDC EM") != std::string::npos ) return HcalZDCDetId::EM;
00175   else if ( _section.find("ZDC HAD") != std::string::npos ) return HcalZDCDetId::HAD;
00176   else if ( _section.find("ZDC LUM") != std::string::npos ) return HcalZDCDetId::LUM;
00177   else return HcalZDCDetId::Unknown;
00178 }
00179 
00180 
00181 
00182 int HcalAssistant::getListOfChannelsFromDb(){
00183   int _n_channels = 0;
00184   static ConnectionManager conn;
00185   conn.connect();
00186   std::string query = "select ";
00187   query            += "       channel_map_id,subdet,ieta,iphi,depth ";
00188   query            += "from ";
00189   query            += "       cms_hcl_hcal_cond.hcal_channels ";
00190   query            += "where ";
00191   query            += "       subdet='HB' or subdet='HE' or subdet='HF' or subdet='HO' ";
00192   try {
00193     oracle::occi::Statement* stmt = conn.getStatement(query);
00194     oracle::occi::ResultSet *rs = stmt->executeQuery();
00195     geom_to_rawid.clear();
00196     rawid_to_geom.clear();
00197     while (rs->next()) {
00198       _n_channels++;
00199       int _rawid = rs->getInt(1);
00200       int _geomId = getGeomId( getSubdetector(rs->getString(2)),
00201                                rs->getInt(3),
00202                                rs->getInt(4),
00203                                rs->getInt(5)
00204                                );
00205       geom_to_rawid.insert(std::pair<int, int>(_geomId, _rawid));
00206       rawid_to_geom.insert(std::pair<int, int>(_rawid, _geomId));
00207     }
00208     listIsRead = true;
00209   }
00210   catch (SQLException& e) {
00211     std::cerr << ::toolbox::toString("Oracle  exception : %s",e.getMessage().c_str()) << std::endl;
00212     XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,::toolbox::toString("Oracle  exception : %s",e.getMessage().c_str()));
00213   }
00214   conn.disconnect();
00215   return _n_channels;
00216 }
00217 
00218 
00219 int HcalAssistant::getGeomId(int _rawid){
00220   if (listIsRead){
00221     std::map<int,int>::const_iterator _geomId = rawid_to_geom.find(_rawid);
00222     if (_geomId!=rawid_to_geom.end()){
00223       return _geomId->second;
00224     }
00225     else return -1;
00226   }
00227   else return -1;
00228 }
00229 
00230 
00231 
00232 int HcalAssistant::getRawId(int _geomId){
00233   if (listIsRead){
00234     std::map<int,int>::const_iterator _rawid = geom_to_rawid.find(_geomId);
00235     if (_rawid!=geom_to_rawid.end()){
00236       return _rawid->second;
00237     }
00238     else return -1;
00239   }
00240   else return -1;
00241 }
00242 
00243 
00244 int HcalAssistant::getRawIdFromCmssw(int _geomId){
00245   std::string s_det = getSubdetectorString(_geomId);
00246   if ( s_det.find("HB") != std::string::npos ||
00247        s_det.find("HE") != std::string::npos ||
00248        s_det.find("HF") != std::string::npos ||
00249        s_det.find("HO") != std::string::npos ||
00250        s_det.find("HT") != std::string::npos )
00251     {
00252       HcalDetId _id( getSubdetector(s_det),
00253                      getHcalIeta(_geomId),
00254                      getHcalIphi(_geomId),
00255                      getHcalDepth(_geomId)
00256                      );
00257       return _id.rawId();
00258     }
00259   else if ( s_det.find("ZDC") != std::string::npos )
00260     {
00261       // FIXME: implement for ZDC channels
00262       return -1;
00263     }
00264   else{
00265       return -1;
00266   }
00267 }
00268 
00269 
00270 int HcalAssistant::getSubdetector(int _rawid){
00271   return getHcalSubdetector( getGeomId(_rawid) );
00272 }
00273 
00274 
00275 int HcalAssistant::getIeta(int _rawid){
00276   return getHcalIeta( getGeomId(_rawid) );
00277 }
00278 
00279 
00280 
00281 int HcalAssistant::getIphi(int _rawid){
00282   return getHcalIphi( getGeomId(_rawid) );
00283 }
00284 
00285 
00286 
00287 int HcalAssistant::getDepth(int _rawid){
00288   return getHcalDepth( getGeomId(_rawid) );
00289 }
00290 
00291 
00292 
00293 int HcalAssistant::getRawId(HcalSubdetector _det, int _ieta, int _iphi, int _depth){
00294   return getRawId( getGeomId(_det, _ieta, _iphi, _depth) );
00295 }
00296 
00297 
00298 
00299 
00300 int HcalAssistant::a_to_i(char * inbuf){
00301   int result;
00302   sscanf(inbuf,"%d",&result);
00303   return result;
00304 }