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