CMS 3D CMS Logo

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

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