CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/DetectorDescription/Core/src/DDValue.cc

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