CMS 3D CMS Logo

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

Go to the documentation of this file.
00001 #include "DetectorDescription/Core/interface/DDStreamer.h"
00002 
00003 //#include "DetectorDescription/Core/interface/DDCompactView.h"
00004 #include "DetectorDescription/Base/interface/Singleton.h"
00005 #include "DetectorDescription/Core/interface/DDMaterial.h"
00006 #include "DetectorDescription/Core/interface/DDSolid.h"
00007 #include "DetectorDescription/Core/interface/DDLogicalPart.h"
00008 #include "DetectorDescription/Core/interface/DDTransform.h"
00009 #include "DetectorDescription/Core/interface/DDRoot.h"
00010 #include "DetectorDescription/Core/interface/DDSpecifics.h"
00011 #include "DetectorDescription/Core/interface/DDValue.h"
00012 #include "DetectorDescription/Base/interface/DDdebug.h"
00013 #include "DetectorDescription/Core/interface/DDConstant.h"
00014 #include "DetectorDescription/Core/interface/DDPartSelection.h"
00015 #include "DetectorDescription/ExprAlgo/interface/ExprEvalSingleton.h"
00016 
00017 // Message logger.
00018 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00019 
00020 #include<iomanip>
00021 
00022 DDStreamer::DDStreamer()
00023  : cpv_(0), o_(0), i_(0)
00024  {
00025  }
00026 
00027 DDStreamer::DDStreamer(std::ostream & os)
00028  :  cpv_(0), o_(0), i_(0)
00029 {
00030   if (os) {
00031     o_ = &os;
00032   }
00033   else {
00034     throw DDException("DDStreamer::DDStreamer(std::ostream&): not valid std::ostream");
00035   }
00036 }
00037 
00038 DDStreamer::DDStreamer(std::istream & is)
00039  :  cpv_(0), o_(0), i_(0)
00040 {
00041   if (is) {
00042     i_ = &is;
00043   }
00044   else {
00045     throw DDException("DDStreamer::DDStreamer(std::ostream&): not valid std::ostream");
00046   }
00047 }
00048 
00049 DDStreamer::~DDStreamer() {}
00050 /*  
00051 DDName dd_get_name(std::istream & is)
00052 {
00053    size_t nm(0), ns(0);
00054    is >> nm;
00055    is >> ns;
00056    return DDName(std::make_pair(nm,ns));
00057 }
00058 
00059 void nameout(std::ostream & o, const DDName & n)
00060 {
00061   o << n.id().first << ' ' << n.id().second;
00062 }
00063 */
00064 
00065 
00066 std::string dd_get_delimit(std::istream & is, char d)
00067 {
00068   std::string nm;
00069   char i;
00070   while ((i=is.get()) && i != d) ;
00071   while ((i=is.get()) && i != d) {
00072       nm = nm + i; 
00073    }
00074   return nm;
00075 }
00076 
00077 DDName dd_get_name_string(std::istream & is)
00078 {  
00079    std::string nm(dd_get_delimit(is,'"'))  ;
00080    return DDName(nm,
00081                  dd_get_delimit(is,'"'));
00082 /*
00083    char i;
00084    while ((i=is.get()) && i != '"') ;
00085    while ((i=is.get()) && i != '"') {
00086       nm = nm + i; 
00087    }
00088    std::pair<std::string,std::string> p = DDSplit(nm);
00089    return DDName(p.first,p.second);
00090 */   
00091 }
00092 
00093 DDName dd_get_name(std::istream & is)
00094 {
00095   size_t id(0);
00096   is >> id;
00097   return DDName(id);
00098 }
00099 
00100 void nameout_strings(std::ostream & o, const DDName & n)
00101 {
00102   o << '"' << n.name() << '"' << ' ' << '"' << n.ns() << '"' ;
00103 }
00104  
00105 void nameout(std::ostream & o, const DDName & n)   
00106 {
00107   o << n.id();
00108 }
00109 
00110 
00111 void DDStreamer::write()
00112 {
00113    if (o_ && *o_) {
00114      write(*o_);
00115    }
00116    else {
00117      throw DDException("DDStreamer::write(): bad std::ostream");
00118    }
00119 }
00120 
00121 void DDStreamer::read()
00122 {
00123    if (i_ && *i_) {
00124     read(*i_);
00125    }
00126    else {
00127      throw DDException("DDStreamer::read(): bad std::istream");
00128    }
00129 }
00130 
00131 void DDStreamer::write(std::ostream & os)
00132 {
00133   o_=&os;
00134   std::streamsize prec(os.precision());
00135   os << std::setprecision(26) << std::scientific;
00136   names_write();
00137   vars_write();  
00138   
00139   materials_write();
00140   solids_write();
00141   parts_write();
00142   
00143   pos_write();
00144   specs_write();
00145   
00146   rots_write();    
00147   //os << DDI::Singleton<DDName::IdToName>::instance().size() << std::endl;
00148   //names_write();
00149   os << resetiosflags((std::ios_base::fmtflags)0);
00150   os << std::setprecision(prec);
00151 }
00152 
00153 
00154 void DDStreamer::read(std::istream & is)
00155 {
00156 
00157   i_=&is;
00158   names_read();
00159   vars_read();
00160   
00161   materials_read();
00162   solids_read();
00163   parts_read();
00164   
00165   pos_read();
00166   specs_read();
00167   rots_read();        
00168 }
00169 
00170 
00171 void DDStreamer::names_write()
00172 {
00173   DCOUT('Y', "DDStreamer::names_write()");
00174   std::ostream & os = *o_;
00175   DDName::IdToName & ids = DDI::Singleton<DDName::IdToName>::instance();
00176   
00177   DDName::IdToName::const_iterator it(ids.begin()), ed(ids.end());
00178   os << ids.size() << std::endl;
00179   size_t count(0);
00180   for (; it != ed; ++it) {
00181     os << '"' << (*it)->first.first << '"' << ' ' 
00182        << '"' << (*it)->first.second << '"' << ' ' 
00183        << count << std::endl;
00184     ++count;
00185   }
00186   
00187 }
00188 
00189 
00190 void DDStreamer::names_read()
00191 {
00192   DCOUT('Y', "DDStreamer::names_read()");
00193   std::istream & is = *i_;
00194   DDName::IdToName & ids = DDI::Singleton<DDName::IdToName>::instance();
00195   DDName::Registry & reg = DDI::Singleton<DDName::Registry>::instance();
00196   
00197   size_t s;
00198   is >> s;
00199   ids.clear();
00200   //ids.resize(s);
00201   reg.clear();
00202   size_t i(0);
00203   //std::string nm; getline(is,nm);
00204   for (; i<s; ++i) {
00205     std::string nm(dd_get_delimit(is,'"'));
00206     std::string ns(dd_get_delimit(is,'"'));
00207     size_t id(0);
00208     is >> id;
00209     DDName::defineId(std::make_pair(nm,ns),id);
00210   }
00211 }
00212 
00213 
00214 template<class T> 
00215 size_t dd_count(const T & dummy)
00216 {
00217   size_t result(0);
00218   typename T::template iterator<T> it(T::begin()), ed(T::end());
00219   for (; it!=ed; ++it) {
00220     if (it->isDefined().second) {
00221       ++result;
00222     }
00223   }
00224   return result;
00225 }
00226 
00227 
00228 struct double_binary
00229 {
00230   explicit double_binary(double d) : val_(d) { }
00231   double_binary() : val_(0) { }
00232   double val_;  
00233 };
00234 
00235 typedef double_binary B;
00236 
00237 std::ostream & operator<<(std::ostream & os, double_binary b)
00238 {
00239   const char * d = (const char *)(&(b.val_));
00240   //size_t s(sizeof(double)), i(0);
00241   //os << '(';
00242   os.write(d,sizeof(double));
00243   return os;
00244 }
00245 
00246 
00247 inline std::istream & operator>>(std::istream & is, double_binary & b)
00248 {
00249   char * d = (char *)(&(b.val_));
00250   //size_t s(sizeof(double)), i(0);
00251   is.read(d,sizeof(double));
00252   return is;
00253 } 
00254 
00255 
00256 void DDStreamer::materials_write()
00257 {
00258   DCOUT('Y', "DDStreamer::materials_write()");
00259   std::ostream & os = *o_;
00260   DDMaterial::iterator<DDMaterial> it(DDMaterial::begin()), ed(DDMaterial::end());
00261   size_t no = dd_count(DDMaterial());
00262   os << no << std::endl;
00263   for (; it != ed; ++it) {
00264     if (! it->isDefined().second) continue;
00265     const DDMaterial & m = *it;
00266     os << "--Material: " << m.name() << " @ " ;
00267     nameout(os,m.name()); 
00268     DCOUT('y', "write-material=" << m.name());
00269     os << ' ' << m.z() << ' ' << m.a() << ' ' << m.density() << ' ';
00270     
00271     int noc = m.noOfConstituents();
00272     os << noc;
00273     int j=0;
00274     for (; j<noc; ++j) {
00275       DCOUT('y', "  write-const-material=" << m.constituent(j).first.name());
00276       os << ' ';
00277       nameout(os,m.constituent(j).first.name());
00278       os << ' ' << m.constituent(j).second;
00279     }
00280     os << std::endl;
00281   }
00282 }
00283 
00284 
00285 void DDStreamer::materials_read()
00286 {
00287   DCOUT('Y', "DDStreamer::materials_read()");
00288   std::istream & is = *i_;
00289   //DDMaterial::clear();
00290   size_t n=0;
00291   is >> n;
00292   size_t i=0;
00293   for (; i < n; ++i) { // Materials
00294     is.ignore(1000,'@');
00295     DDName dn = dd_get_name(is);
00296     double z(0), a(0), d(0);
00297     is >> z;
00298     is >> a;
00299     is >> d;
00300     int comp(0);
00301     is >> comp; // composites
00302     if (comp) { // composite material
00303       DDMaterial m(dn,d);
00304       DCOUT('y', "read-comp-material=" << m.name());
00305       int j=0;
00306       for(; j<comp; ++j) {
00307         DDName cname(dd_get_name(is));
00308         double fm(0);
00309         is >> fm;
00310         DDMaterial constituent(cname);
00311         DCOUT('y', "  read-composite=" << constituent.name());
00312         m.addMaterial(constituent,fm);
00313       }
00314     }
00315     else { // elementary material
00316       DDMaterial m(dn,z,a,d);
00317       DCOUT('y', "read-elem-material=" << m.name());
00318     }
00319   }
00320 }
00321 
00322 void dd_stream_booleans(std::ostream& os, DDSolid s, DDSolidShape sh)
00323 {
00324   DDBooleanSolid b(s);
00325   DDRotation temprot = b.rotation();
00326   if(!temprot.isDefined().second) {
00327     temprot = DDRotation();
00328     edm::LogError("DDStreamer") << "DDStreamer::dd_stream_booleans(): solid=" << s.name() << " has no rotation. Using unit-rot." << std::endl;
00329   }
00330   nameout(os,temprot.name()); 
00331   // binary output of the translation std::vector
00332   os << ' ' << B(b.translation().x()) // << ' '
00333      << B(b.translation().y()) // << ' '
00334      << B(b.translation().z()) << ' '; 
00335   nameout(os,b.solidA().name());
00336   os << ' ';
00337   nameout(os,b.solidB().name());            
00338 }
00339 
00340 void dd_stream_reflected(std::ostream & os, DDSolid s)
00341 {
00342       DDReflectionSolid ref(s);
00343       nameout(os,ref.unreflected().name());
00344 }
00345 
00346 void DDStreamer::solids_write()
00347 {
00348   DCOUT('Y', "DDStreamer::solids_write()");
00349   std::ostream & os = *o_;
00350   DDSolid::iterator<DDSolid> it(DDSolid::begin()), ed(DDSolid::end());
00351   size_t no = dd_count(DDSolid());
00352   os << no << std::endl;
00353   for (; it != ed; ++it) {
00354     if (! it->isDefined().second) continue;  
00355     const DDSolid & s = *it;
00356     DCOUT('y', "write-solid=" << s << " enum=" << s.shape());
00357     os << "--Solid: " << s.name() << ' ' << DDSolidShapesName::name(s.shape()) << " @ ";
00358     nameout(os,s.name()); 
00359     os << ' ' << s.shape() << ' ';
00360     switch (s.shape()) {
00361     case ddunion: case ddsubtraction: case ddintersection:
00362       dd_stream_booleans(os, s, s.shape());
00363       break;
00364     case ddreflected:
00365       dd_stream_reflected(os, s);
00366       break;
00367     default:
00368       size_t ps = s.parameters().size();
00369       os << ps;
00370       const std::vector<double> & p = s.parameters();
00371       os << ' ';
00372       os.write((char*)(&(*p.begin())),ps*sizeof(double));
00373       /*
00374       std::vector<double>::const_iterator it(p.begin()), ed(p.end());
00375       for (; it != ed; ++it) {
00376         os << ' ' << *it;
00377       }
00378       */
00379     }
00380     os << std::endl;
00381   }
00382 }
00383 
00384 
00385 
00386 void dd_get_boolean_params(std::istream & is, DDRotation & r, DDTranslation & t, DDSolid & a, DDSolid & b)
00387 {
00388    DDName n = dd_get_name(is);
00389    r = DDRotation(n);
00390    //double x(0), y(0), z(0);
00391    B x,y,z;
00392    char cr = is.get();
00393    if(cr != ' ') 
00394       throw DDException("DDStreamer::get_boolean_param(): inconsistent sequence! no blank delimiter before trans!");
00395    is >> x;
00396    is >> y;
00397    is >> z;
00398    t = DDTranslation(x.val_,y.val_,z.val_);
00399    n = dd_get_name(is);
00400    a = DDSolid(n);
00401    n = dd_get_name(is);
00402    b = DDSolid(n);
00403    DCOUT('y', "boolean-par: rot=" << r.name() << " t=" << t << " a=" << a.name() << " b=" << b.name());
00404 }
00405 
00406 void DDStreamer::solids_read()
00407 {
00408   DCOUT('Y', "DDStreamer::solids_read()");
00409   std::istream & is = *i_;
00410   //DDSolid::clear();
00411   size_t n=0;
00412   is >> n;
00413   size_t i=0;
00414   for (; i < n; ++i) { // Solids
00415     is.ignore(1000,'@');
00416     DDName dn = dd_get_name(is);
00417 
00418     size_t sp(0);
00419     is >> sp;
00420     DDSolidShape shape = DDSolidShape(sp);
00421     
00422     // boolean solids
00423     if ( (shape==ddunion) | (shape==ddsubtraction) || (shape==ddintersection) ) {
00424       DDRotation r;
00425       DDTranslation t;
00426       DDSolid a;
00427       DDSolid b;
00428       dd_get_boolean_params(is,r,t,a,b);
00429       switch (shape) {
00430       case ddunion:
00431         DDSolidFactory::unionSolid(dn,a,b,t,r);
00432         break;
00433       case ddintersection:
00434         DDSolidFactory::intersection(dn,a,b,t,r);
00435         break;
00436       case ddsubtraction:
00437         DDSolidFactory::subtraction(dn,a,b,t,r);
00438         break;  
00439       default:
00440         throw DDException("DDStreamer::solids_read(): messed up in boolean solid reading!");    
00441       }
00442     }
00443     
00444     // reflection solids
00445     else if (shape==ddreflected) {
00446       DDName ref_nm = dd_get_name(is);
00447       DDSolidFactory::reflection(dn,ref_nm);
00448     }
00449     else if ( (shape==ddbox ) ||
00450               (shape==ddtrap) ||
00451               (shape==ddcons) ||
00452               (shape==ddtubs) ||    
00453               (shape==ddpolycone_rz) ||
00454               (shape==ddpolycone_rrz) ||
00455               (shape==ddpolyhedra_rz) ||                          
00456               (shape==ddpolyhedra_rrz) ||
00457               (shape==ddpseudotrap) ||
00458               (shape==ddshapeless) )
00459     {
00460       // read in the solid's parameters
00461       size_t npars(0);
00462       is >> npars;
00463       
00464       std::vector<double> p(npars);
00465       if(npars) {
00466         //edm::LogError("DDStreamer") << npars << flush << std::endl;
00467         char c;
00468         c = is.get();
00469         if (c != ' ') {
00470            edm::LogError("DDStreamer") << "delimiter: " << c << std::endl;
00471           throw DDException("DDStreamer::solids_read(): wrong separator in atomic for atomic solids parameters");
00472         }
00473         is.read((char*)&(*(p.begin())),npars*sizeof(double));   
00474         /*
00475         size_t i(0);
00476         for(; i< npars; ++i) {
00477           double d(0);
00478           is >> d;
00479           p.push_back(d);
00480         }
00481         */
00482       } 
00483       DDSolid so = DDSolid(dn,shape,p);
00484       DCOUT('y', "read-solid=" << so);     
00485     }
00486     else {
00487       edm::LogError("DDStreamer") << "wrong solid enum: " << shape << std::endl;
00488       throw DDException("Error in DDStreamer::solids_read(), wrong shape-enum!");
00489     }
00490   }
00491 }
00492 
00493 void DDStreamer::parts_write()
00494 {
00495   DCOUT('Y', "DDStreamer::parts_write()");
00496   std::ostream & os = *o_;
00497   DDLogicalPart::iterator<DDLogicalPart> it(DDLogicalPart::begin()), ed(DDLogicalPart::end());
00498   size_t no = dd_count(DDLogicalPart());
00499   os << no << std::endl;
00500   for (; it != ed; ++it) {
00501     if (! it->isDefined().second) continue;  
00502     const DDLogicalPart & lp = *it;
00503     os << "--Part: " << lp.name() << " @ ";
00504     nameout(os,lp.name()); 
00505     os << ' ' << lp.category() << ' ';
00506     nameout(os,lp.material().name());
00507     os << ' ';
00508     nameout(os,lp.solid().name());
00509     os << std::endl;
00510   }
00511 }
00512 
00513 
00514 void DDStreamer::parts_read()
00515 {
00516   DCOUT('Y', "DDStreamer::parts_read()");
00517   std::istream & is = *i_;
00518   //DDLogicalPart::clear();
00519   size_t n=0;
00520   is >> n;
00521   size_t i=0;
00522   for (; i < n; ++i) { // LogicalParts
00523     is.ignore(1000,'@');
00524     DDName dn = dd_get_name(is);
00525     size_t cat(0);
00526     is >> cat;
00527     DDEnums::Category categ = DDEnums::Category(cat);
00528     DDName mat = dd_get_name(is);
00529     DDName sol = dd_get_name(is);
00530     DDLogicalPart lp(dn,mat,sol,categ);
00531     DCOUT('y', "read-lp=" << lp);
00532   }
00533 }
00534 
00535 void dd_rot_bin_out(std::ostream & os, const DDRotationMatrix & rm)
00536 {
00537   double v[9]; 
00538   rm.GetComponents(v,v+9);
00539   for (int i=0;i<9;i++)
00540     os        << B(v[i]);
00541 }
00542 
00543 void dd_rot_out(std::ostream & os, const DDRotation & r) {
00544     os << "--Rot: " << r.name() << " @ ";
00545     nameout(os,r.name());
00546     os << ' ';
00547     const DDRotationMatrix & rm = *(r.rotation());
00548     DCOUT('y', "write-rots=" << r.name());
00549 /*
00550     os << ' ' << B(rep.xx_) << ' ' << B(rep.xy_) << ' ' << B(rep.xz_) << ' '
00551               << B(rep.yx_) << ' ' << B(rep.yy_) << ' ' << B(rep.yz_) << ' '
00552               << B(rep.zx_) << ' ' << B(rep.zy_) << ' ' << B(rep.zz_) << std::endl; 
00553 */            
00554    dd_rot_bin_out(os,rm);             
00555    os << std::endl;
00556 }
00557 
00558 void DDStreamer::rots_write()
00559 { 
00560   DCOUT('Y', "DDStreamer::rots_write()");
00561   std::ostream & os = *o_;
00562   DDRotation::iterator<DDRotation> it(DDRotation::begin()), ed(DDRotation::end());
00563   size_t no = dd_count(DDRotation());
00564   os << no << std::endl;
00565   //DDName ano;
00566   for (; it != ed; ++it) {
00567     if (! it->isDefined().second) continue;  
00568     const DDRotation & r = *it;
00569     //if (r.name().id() == ano.id()) {
00570     //  continue;
00571     //}
00572     dd_rot_out(os,r);   
00573   } 
00574 }
00575 
00576 
00577 void dd_rot_bin_in(std::istream & is, DDRotationMatrix & r)
00578 {
00579     double v[9];
00580     B w;
00581     for (int i=0; i<9;i++) {
00582       is >> w; v[i]=w.val_;
00583     }
00584     r.SetComponents(v,v+9);
00585 }
00586 
00587 void DDStreamer::rots_read()
00588 {
00589   DCOUT('Y', "DDStreamer::rots_read()");
00590   std::istream & is = *i_;
00591   //DDRotation::clear();
00592   size_t n=0;
00593   is >> n;
00594   size_t i=0;
00595   for (; i < n; ++i) { // Rotations
00596     is.ignore(1000,'@');
00597     DDName dn = dd_get_name(is);
00598     char c = is.get();
00599     if (c != ' ') { 
00600       throw DDException("DDStreamer::rots_read(): inconsitency! no blank separator found!");
00601     }
00602  
00603     DDRotationMatrix * rm = new DDRotationMatrix();
00604     dd_rot_bin_in(is,*rm);
00605     DDRotation ddr = DDRotation(dn,rm);
00606     DCOUT('y',"read-rots=" << ddr.name());
00607   }
00608 }
00609 
00610 void DDStreamer::pos_write()
00611 {
00612   DCOUT('Y', "DDStreamer::pos_write()");
00613   DDCompactView cpv;
00614   const DDCompactView::graph_type & g = cpv.graph();
00615   DDCompactView::graph_type::const_iterator it = g.begin_iter();
00616   DDCompactView::graph_type::const_iterator ed = g.end_iter();
00617   std::ostream & os = *o_;
00618   // first the root
00619   DDLogicalPart rt = DDRootDef::instance().root();
00620   os << "--Root: @ ";
00621   nameout(os,rt.name());
00622   os << std::endl;
00623   //os << g.edge_size() << std::endl;
00624   DDCompactView::graph_type::const_iterator iit = g.begin_iter();
00625   DDCompactView::graph_type::const_iterator eed = g.end_iter();
00626   size_t count(0);
00627   for(; iit != eed; ++iit) {
00628     ++count;
00629   }
00630   os << count << std::endl;
00631   count=0;
00632   DDName unit_rot_name;
00633   DDRotationMatrix unit_rot;
00634   for(; it != ed; ++it) {
00635      os << "--Pos[" << count << "]: @ "; ++count;
00636      //const DDLogicalPart & fr = it->from(); 
00637      nameout(os, it->from().name());
00638      os << ' ';
00639      nameout(os, it->to().name());
00640      os << ' ' << it->edge()->copyno_;
00641      const DDTranslation & tr = it->edge()->translation();
00642      //os << ' ' << B(tr.x()) << ' ' << B(tr.y()) << ' ' << B(tr.z());
00643      os << ' ' << B(tr.x()) << B(tr.y()) << B(tr.z());
00644      const DDRotation & ro = it->edge()->rot_;
00645      os << ' ';
00646      /* if it's an anonymous rotation stemming from an AlgoPosPart
00647         then it's id must be the one of a unit rotation AND
00648         it must be defined at this point AND it must not be the
00649         unit rotation.
00650         A character identifier is issues to mark the type of the rotation:
00651         a ... anonymous rotation, followed by the binary numbers of the matrix
00652         u ... unit-rotation matrix, no values following
00653         r ... regular defined rotation-matrix (named rotation matrix) followed by name-id
00654      */
00655      if (ro.name() == unit_rot_name) {
00656        if(ro.isDefined().second) {
00657          if(*(ro.rotation()) != unit_rot) {
00658            os << "a ";
00659            dd_rot_bin_out(os,*(ro.rotation()));
00660          }  
00661          else {
00662            os << "u ";
00663          }
00664        }
00665      }
00666      else {
00667        os << "r ";
00668        nameout(os, ro.name());
00669      }  
00670      os << std::endl;
00671   }
00672 
00673 }
00674 
00675 
00676 void DDStreamer::pos_read()
00677 {
00678   DCOUT('Y', "DDStreamer::pos_read()");
00679   std::istream & is = *i_;
00680   is.ignore(1000,'@');
00681   DDName rtname = dd_get_name(is);
00682   DDLogicalPart root(rtname);
00683   DCOUT('y', "root is: " << root.name());
00684   DDRootDef::instance().set(root);
00685   size_t n=0;
00686   is >> n;
00687   size_t i=0;
00688   DDCompactView cpv;
00689   DDCompactView::graph_type & g = const_cast<DDCompactView::graph_type&>(cpv.graph());
00690   //  DDPositioner pos_(&cpv);
00691   //LogDebug << "===== GRAPH SIZE = " << g.size() << " ======" << std::endl << std::endl;
00692   if (g.size()) {
00693     edm::LogWarning("DDStreamer") << std::endl;
00694     edm::LogWarning("DDStreamer") << "DDStreamer::pos_read(): The CompactView already contains some position information." << std::endl
00695          << "                        It may cause an inconsistent geometry representation!" << std::endl << std::endl;
00696     throw DDException("DDStreamer::pos_read() failed; CompactView has already been populated by another data source");   
00697   }
00698   for (; i < n; ++i) { // Positions
00699     is.ignore(1000,'@');
00700     DDName from(dd_get_name(is));
00701     DDName to(dd_get_name(is));
00702     std::string cp;
00703     is >> cp;
00704     char cr = is.get();
00705     if (cr != ' ') throw DDException("DDStreamer::pos_read(): inconsistent sequence! no blank delimiter found!");
00706     //double x,y,z;
00707     B x,y,z;
00708     is >> x;
00709     is >> y;
00710     is >> z;
00711     DDTranslation t(x.val_,y.val_,z.val_);
00712     is.ignore();
00713     char rottype = is.get();
00714     DDRotationMatrix * matrix(0);
00715     //DDName rotname;
00716     DDRotation rot;
00717     switch(rottype) {
00718       case 'a': // anonymous rotation
00719         is.ignore();
00720         matrix = new DDRotationMatrix;
00721         dd_rot_bin_in(is,*matrix);
00722         rot = DDanonymousRot(matrix);
00723         break;
00724       case 'u': // unit rotation
00725         break;
00726       case 'r': // regular (named) rotation
00727         rot = DDRotation(dd_get_name(is));
00728         break;
00729       default:
00730         std::string message = "DDStreamer::pos_read(): could not determine type of rotation\n";
00731         throw(DDException(message));
00732       }                                
00733     //DDName rot(dd_get_name(is));
00734     cpv.position(DDLogicalPart(to),DDLogicalPart(from),cp,t,rot); 
00735     DCOUT('y', " pos-read: f=" << from << " to=" << to << " t=" << t << " r=" << rot);
00736   }
00737 }
00738 
00739 /*
00740 void dd_ps_out(std::ostream & os, DDPartSelection* p)
00741 {
00742    size_t i(0),j(p->size());
00743    os << ' ' << j << ' ';
00744    for (; i<j; ++i) {
00745      nameout(os,(*p)[j].lp_.name());
00746      os << ' ';
00747      
00748    }
00749 }
00750 
00751 void DDStreamer::specs_write()
00752 {
00753   DCOUT('Y', "DDStreamer::parts_write()");
00754   std::ostream & os = *o_;
00755   DDLogicalPart::iterator<DDLogicalPart> it(DDLogicalPart::begin()), ed(DDLogicalPart::end());
00756   size_t no = DDLogicalPart::size();
00757   os << no << std::endl;
00758   for (; it != ed; ++it) {
00759     const DDLogicalPart & lp = *it;
00760     const std::vector< std::pair<DDPartSelection*,DDsvalues_type> > & sp = lp.attachedSpecifics();
00761     if ((size_t s = sp.size())) {
00762       os << "--Specs " << lp.name() << " @ " << s << ' ';
00763       size_t j=0;
00764       for (; j<s; ++j) {
00765         dd_ps_out(os,s[j].first);
00766         DDsvalues_type* sv = s[j].second;
00767         os << ' ' << sv->size() << ' ';
00768         dd_sv_out(os,sv);
00769       }
00770     }
00771 }
00772 */
00773 
00774 void DDStreamer::specs_write()
00775 {
00776   DCOUT('Y', "DDStreamer::specs_write()");
00777   std::ostream & os = *o_;
00778   DDSpecifics::iterator<DDSpecifics> it(DDSpecifics::begin()), ed(DDSpecifics::end());
00779   size_t no = dd_count(DDSpecifics());
00780   os << no << std::endl;
00781   for (; it != ed; ++it) {
00782     if (! it->isDefined().second) continue;  
00783     const DDSpecifics & sp = *it;
00784     os << "--Spec: @ ";
00785     nameout(os,sp.name());
00786     os << ' ' << sp.selection().size() << std::endl;
00787     std::vector<DDPartSelection>::const_iterator sit(sp.selection().begin()), sed(sp.selection().end());
00788     for (; sit != sed; ++sit) {
00789       os << *sit << std::endl;
00790     }
00791     os << sp.specifics().size() << std::endl;
00792     DDsvalues_type::const_iterator vit(sp.specifics().begin()), ved(sp.specifics().end());
00793     for (; vit != ved; ++vit) {
00794       const DDValue & v = vit->second;
00795       os << ' ' << '"' << v.name() << '"' << ' ';
00796       if (v.isEvaluated()) {
00797         os << 1 << ' ';
00798       }
00799       else {
00800         os << 0 << ' ';
00801       }
00802       os << v.size() << ' ';
00803       if (v.isEvaluated()) {
00804         size_t s=v.size();
00805         size_t i=0;
00806         for (; i<s; ++i) {
00807           os << '"' << v[i].first << '"' << ' ' << v[i].second << ' ';
00808         }
00809       }
00810       else {
00811         size_t s=v.size();
00812         size_t i=0;
00813         const std::vector<std::string> & vs = v.strings();
00814         for (; i<s; ++i) {
00815           os << '"' << vs[i] << '"' << ' ';
00816         }
00817       }
00818       os << std::endl;
00819       
00820     }
00821   }  
00822 }
00823 
00824 void DDStreamer::specs_read()   
00825 {
00826   DCOUT('Y', "DDStreamer::specs_read()");
00827   std::istream & is = *i_;
00828   //DDSpecifics::clear();
00829   size_t n=0;
00830   is >> n;
00831   size_t i=0;
00832   for (; i < n; ++i) { // Specifics
00833     is.ignore(1000,'@');
00834     DDName sn(dd_get_name(is));
00835     size_t nps(0);
00836     is >> nps;
00837     size_t ii=0;
00838     std::vector<std::string> ps;
00839     is.ignore(100,'\n');
00840     for (; ii < nps; ++ii) {
00841       std::string s;
00842       getline(is,s);
00843       DCOUT('y', "specs-ps=" << s);
00844       ps.push_back(s);
00845     }
00846     is >> nps;
00847     ii=0;
00848     DDsvalues_type sv;
00849     for(; ii<nps; ++ii) {
00850       std::string name = dd_get_delimit(is,'"');
00851       bool evl(false);
00852       is >> evl;
00853       size_t no(0);
00854       is >> no;
00855       std::vector<DDValuePair> valv;
00856       DDValue result;
00857       if (evl) {
00858         size_t iii=0;
00859         for(; iii<no; ++iii) {
00860           std::string strv = dd_get_delimit(is,'"');
00861           double dblv(0);
00862           is >> dblv;
00863           DDValuePair valp(strv,dblv);
00864           valv.push_back(valp);
00865         }
00866         result = DDValue(name,valv);
00867         result.setEvalState(true);
00868       }
00869       else {
00870         size_t iii=0;
00871         for(; iii<no; ++iii) {
00872           std::string strv = dd_get_delimit(is,'"');    
00873           DDValuePair valp(strv,0);
00874           valv.push_back(valp);
00875         }
00876         result = DDValue(name,valv);
00877         result.setEvalState(false);
00878       }
00879       sv.push_back(DDsvalues_Content_type(result,result));
00880     }
00881     std::sort(sv.begin(),sv.end());
00882     DDSpecifics sp(sn,ps,sv,false);
00883     DCOUT('y', " specs-read: " << sp);
00884   }  
00885 }
00886 
00887 
00888 void DDStreamer::vars_write()
00889 {
00890   std::ostream & os = *o_;
00891   ExprEvalInterface & ev = ExprEvalSingleton::instance();
00892   ClhepEvaluator * eval = dynamic_cast<ClhepEvaluator*>(&ev);
00893   if (eval){
00894     const std::vector<std::string> & vars = eval->variables();
00895     const std::vector<std::string> & vals = eval->values();
00896     if (vars.size() != vals.size()) {
00897       throw DDException("DDStreamer::vars_write(): different size of variable names & values!") ;
00898     }
00899     size_t i(0), s(vars.size());
00900     os << s << std::endl;
00901     for (; i<s; ++i) {
00902       os << '"' << vars[i] << '"' << ' ' 
00903          << '"' << vals[i] << '"' << std::endl; 
00904     }  
00905   }
00906   else {
00907     throw DDException("DDStreamer::vars_write(): expression-evaluator is not a ClhepEvaluator-implementation!");
00908   }
00909 }
00910 
00911 
00912 void DDStreamer::vars_read()
00913 {
00914   DCOUT('Y', "DDStreamer::vars_read()");
00915   std::istream & is = *i_;
00916   ExprEvalInterface & ev = ExprEvalSingleton::instance();
00917   ClhepEvaluator * eval = dynamic_cast<ClhepEvaluator*>(&ev);
00918   if (eval){
00919     size_t n(0);
00920     is >> n;
00921     size_t i(0);
00922   
00923     for(; i<n; ++i) {
00924       std::string name(dd_get_delimit(is,'"'));
00925       std::string value(dd_get_delimit(is,'"'));
00926       eval->set(name,value);
00927     }
00928   }
00929   else {
00930     throw DDException("DDStreamer::vars_write(): expression-evaluator is not a ClhepEvaluator-implementation!");  
00931   }
00932   DDConstant::createConstantsFromEvaluator();
00933 }
00934 
00935