CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/DetectorDescription/Core/src/DDValue.cc

Go to the documentation of this file.
00001 #include "DetectorDescription/Core/interface/DDValue.h"
00002 
00003 #include "DetectorDescription/Base/interface/DDException.h"
00004 
00005 // Message logger.
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 
00008 #include <cassert>
00009 
00010 //map<std::string,unsigned int> DDValue::indexer_;
00011 //std::vector<std::string> DDValue::names_(1);
00012 void DDValue::init(const std::string &name) {
00013   unsigned int temp = indexer().size()+1;
00014   typedef std::map<std::string,unsigned int>::iterator itT;
00015   std::pair<itT,bool> result = indexer().insert(std::make_pair(name,temp));
00016   
00017   if (result.second) {
00018     id_ = temp;
00019     names().push_back(name);
00020   }
00021   else {
00022     id_ = result.first->second;
00023   }  
00024 }
00025 
00026 
00027 
00028 DDValue::DDValue(const std::string & name)
00029  : id_(0)
00030 {
00031   init(name);
00032 }
00033 
00034 DDValue::DDValue(const char * name)
00035  : id_(0)
00036 {
00037   init(name);
00038 }
00039 
00040 DDValue::DDValue(const std::string & name, const std::vector<DDValuePair>& v) 
00041  : id_(0)
00042 {
00043   init(name);
00044   
00045   std::vector<DDValuePair>::const_iterator it = v.begin();
00046   std::vector<std::string> svec;
00047   std::vector<double> dvec;
00048   vecPair_ = new vecpair_type(false,std::make_pair(svec,dvec));
00049   mem(vecPair_);
00050   for (; it != v.end(); ++it) {
00051     vecPair_->second.first.push_back(it->first);
00052     vecPair_->second.second.push_back(it->second);
00053   }
00054   
00055   //valPairs_ = new std::vector<DDValuePair>(v);
00056 }
00057 
00058 
00059 DDValue::DDValue(const std::string & name, double val) 
00060  : id_(0) {
00061   init(name);
00062   
00063   std::vector<std::string> svec(1,""); 
00064   std::vector<double> dvec(1,val);
00065   
00066   vecPair_ =  new vecpair_type(false,std::make_pair(svec,dvec));
00067   setEvalState(true);
00068   mem(vecPair_);
00069   
00070   //DDValuePair vp(val);
00071   //valPairs_ = new std::vector<DDValuePair>(1,vp);
00072 }
00073 
00074 
00075 DDValue::DDValue(const std::string & name, const std::string & sval, double dval) 
00076  : id_(0)
00077  {
00078    init(name);
00079   
00080   std::vector<std::string> svec(1,sval);
00081   std::vector<double> dvec(1,dval);
00082   vecPair_ = new vecpair_type(false,std::make_pair(svec,dvec));
00083   setEvalState(true);
00084   mem(vecPair_);
00085   //DDValuePair vp(sval,dval);
00086   //valPairs_ = new std::vector<DDValuePair>(1,vp);
00087 }
00088 
00089 
00090 DDValue::DDValue(const std::string & name, const std::string & sval) 
00091  : id_(0)
00092  {
00093    init(name);
00094   
00095   std::vector<std::string> svec(1,sval);
00096   std::vector<double> dvec(1,0);
00097   vecPair_ = new vecpair_type(false,std::make_pair(svec,dvec));
00098   setEvalState(false);
00099   mem(vecPair_);
00100   //DDValuePair vp(sval,dval);
00101   //valPairs_ = new std::vector<DDValuePair>(1,vp);
00102 }
00103 
00104 
00105 DDValue::DDValue(unsigned int i) 
00106  : id_(0)
00107 {
00108  if (names().size()-1 <= i)
00109    id_ = i;
00110 }
00111 
00112 
00113 DDValue::~DDValue()
00114 {
00115   //DCOUT('C' , "Deleting DDValue.name()=" << name() );
00116  // delete vecPair_;
00117 }
00118 
00119 
00120 void DDValue::clear()
00121 {
00122   std::vector<boost::shared_ptr<vecpair_type> > & v = mem(0);
00123   //NOT sure about removing this... I think it is ok because of shared_ptr...
00124 //   std::vector<boost::shared_ptr<vecpair_type> >::iterator it = v.begin();
00125 //   for (; it != v.end(); ++ it) {
00126 //     delete *it;
00127 //   }
00128   v.clear();
00129   //delete vecPair_;
00130   //vecPair_=0;
00131 }
00132 
00133 
00134 std::map<std::string,unsigned int>& DDValue::indexer() { 
00135  static std::map<std::string,unsigned int> indexer_;
00136  return indexer_;
00137 }  
00138   
00139 std::vector<std::string>& DDValue::names() {
00140  static std::vector<std::string> names_(1);
00141  return names_;
00142 } 
00143 
00144 std::vector<boost::shared_ptr<DDValue::vecpair_type> >& DDValue::mem(DDValue::vecpair_type * vp)
00145 {
00146   static std::vector<boost::shared_ptr<vecpair_type> > memory_;
00147   memory_.push_back( boost::shared_ptr<vecpair_type>(vp) );
00148   return memory_;
00149 }
00150 
00151 const std::vector<double> & DDValue::doubles() const 
00152 { 
00153   if (vecPair_->first) {
00154     return vecPair_->second.second; 
00155   }
00156   else {
00157     std::string message = "DDValue " + names()[id_] + " is not numerically evaluated! Use DDValue::std::strings()!";
00158     edm::LogError("DDValue") << message << std::endl;
00159     throw DDException(message);
00160   }
00161 }
00162 
00163 std::ostream & operator<<(std::ostream & o, const DDValue & v)
00164 {
00165   o << v.name() << " = ";
00166   unsigned int i = 0;
00167   if (v.isEvaluated()) {
00168     for (; i<v.size(); ++i) {
00169       o << '(' << v[i].first << ',' << v[i].second << ") ";
00170     }  
00171   } else {
00172     const std::vector<std::string> & s = v.strings();
00173     for (; i<v.size(); ++i) {
00174       o << s[i] << ' ';
00175       //o << '(' << s << ',' << double(v[i].second) << ") ";
00176     }
00177   }  
00178   return o;
00179 }
00180 
00181 //FIXME move it elsewhere; DO NOT put out the name for now... need to fix DDCoreToDDXMLOutput
00182 std::ostream & operator<<(std::ostream & o, const DDValuePair & v) {
00183   return o<< v.second;
00184   //  return o << v.first << ':' << v.second;
00185 }
00186 
00187 
00188 DDValuePair DDValue::operator[](unsigned int i) const
00189   { 
00190     if (vecPair_->first) {
00191       return DDValuePair(vecPair_->second.first[i], vecPair_->second.second[i]);
00192     }
00193     else {
00194       std::string message = "DDValue " + names()[id_] + " is not numerically evaluated! Use DDValue::std::strings()!";
00195       edm::LogError("DDValue") << message;
00196       throw DDException(message);
00197     }
00198   }
00199 
00200 
00201 void DDValue::setEvalState(bool newState)
00202 { 
00203   vecPair_->first = newState; 
00204 }
00205 
00206 
00207 bool DDValue::isEvaluated() const
00208 {
00209   return vecPair_->first;
00210 }
00211 
00212 
00213 bool DDValue::operator==(const DDValue & v) const 
00214 {
00215   bool result(false);
00216   if (id()==v.id()) { 
00217     assert(vecPair_);
00218     assert(v.vecPair_);
00219     if (vecPair_->first) { // numerical values
00220       result = (vecPair_->second.second == v.vecPair_->second.second);
00221     }  
00222     else { // std::string values
00223       result = (vecPair_->second.first == v.vecPair_->second.first);
00224     }
00225   }
00226   return result;
00227 }
00228 
00229 
00230 bool DDValue::operator<(const DDValue & v) const 
00231 {
00232   bool result(false);
00233   if (id()<v.id()) { 
00234     result = true;
00235   }
00236   else {
00237     if (id()==v.id()) {
00238       assert(vecPair_);
00239       assert(v.vecPair_);
00240       if (vecPair_->first && v.vecPair_->first) { // numerical values
00241         result = (vecPair_->second.second < v.vecPair_->second.second);
00242       }  
00243       else { // std::string values
00244         result = (vecPair_->second.first < v.vecPair_->second.first);
00245       }
00246     }
00247   }
00248   return result;
00249 }
00250