CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/DetectorDescription/OnlineDBLoader/bin/stubs/PhysicalPartsTree.cc

Go to the documentation of this file.
00001 #include <memory>
00002 
00003 #include <FWCore/Framework/interface/EDAnalyzer.h>
00004 #include <FWCore/Framework/interface/EventSetup.h>
00005 #include "FWCore/Framework/interface/ESTransientHandle.h"
00006 #include <FWCore/Framework/interface/ESHandle.h>
00007 #include <FWCore/Framework/interface/MakerMacros.h>
00008 
00009 #include <DetectorDescription/Core/interface/DDCompactView.h>
00010 #include <DetectorDescription/Core/interface/DDValue.h>
00011 #include <DetectorDescription/Core/interface/DDExpandedView.h>
00012 #include "DetectorDescription/Core/interface/DDName.h"
00013 #include "DetectorDescription/Core/interface/DDSolid.h"
00014 #include "DetectorDescription/Core/interface/DDMaterial.h"
00015 
00016 #include <Geometry/Records/interface/IdealGeometryRecord.h>
00017 
00018 #include <iostream>
00019 #include <istream>
00020 #include <fstream>
00021 #include <string>
00022 #include <vector>
00023 #include <algorithm>
00024 #include <utility>
00025 
00026 class PhysicalPartsTree : public edm::EDAnalyzer {
00027 
00028 public:
00029  
00030   explicit PhysicalPartsTree( const edm::ParameterSet& );
00031   ~PhysicalPartsTree();
00032   
00033   virtual void analyze( const edm::Event&, const edm::EventSetup& );
00034   virtual void beginRun( const edm::Run&, const edm::EventSetup& );
00035   
00036 private: 
00037 
00038   //std::string filename_;  
00039 };
00040 
00041 PhysicalPartsTree::PhysicalPartsTree( const edm::ParameterSet& iConfig )
00042 {
00043 
00044 }
00045 
00046 
00047 PhysicalPartsTree::~PhysicalPartsTree()
00048 {
00049 }
00050 
00051 void PhysicalPartsTree::analyze( const edm::Event& iEvent, const edm::EventSetup& iSetup ) { 
00052   std::cout << "analyze does nothing" << std::endl;
00053 }
00054 
00055 void PhysicalPartsTree::beginRun( const edm::Run&, const edm::EventSetup& iSetup ) {
00056   //set a tolerance for "near zero"
00057   double tolerance = 1.0e-3;
00058 
00059   std::string physicalPartsTreeFileName("PHYSICALPARTSTREE.dat");
00060   std::string logicalPartTypesFileName("LOGICALPARTTYPES.dat");
00061   std::string nominalPlacementsFileName("NOMINALPLACEMENTS.dat");
00062   std::string detectorPartsFileName("DETECTORPARTS.dat");
00063   std::string pospartsGraphFileName("POSPARTSGRAPH.dat");
00064 
00065   std::ofstream physicalPartsTreeOS(physicalPartsTreeFileName.c_str());
00066   std::ofstream logicalPartTypesOS(logicalPartTypesFileName.c_str());
00067   std::ofstream nominalPlacementsOS(nominalPlacementsFileName.c_str());
00068   std::ofstream detectorPartsOS(detectorPartsFileName.c_str());
00069   std::ofstream pospartsGraphOS(pospartsGraphFileName.c_str());
00070 
00071 
00072   std::string slpname;
00073   std::string scategory;
00074   std::vector<std::string> lpname_vec;
00075 
00076 
00077   std::cout << "PhysicalPartsTree Analyzer..." << std::endl;
00078   edm::ESTransientHandle<DDCompactView> pDD;
00079 
00080   iSetup.get<IdealGeometryRecord>().get( "", pDD );
00081   
00082   const DDCompactView & cpv = *pDD;
00083   DDExpandedView epv(cpv);
00084   int pospartid=0;
00085   size_t lastfound;
00086   std::string lgname;
00087    while(epv.next()){
00088  
00089     //for table physicalpartstree
00090     std::ostringstream parent, child,logicalpartid,parentid;
00091     child << epv.logicalPart().name();
00092     const DDGeoHistory & hist = epv.geoHistory();
00093     parent << hist[hist.size()-2].logicalPart().name();
00094     logicalpartid << epv.geoHistory();
00095     parentid<<hist[hist.size()-2];
00096     lgname=logicalpartid.str();
00097     lastfound=lgname.find_last_of("/\\");
00098     lgname=lgname.substr(lastfound+1,lgname.size());
00099     physicalPartsTreeOS<<lgname<<","<< parentid.str() << "," << child.str()<<std::endl;
00100 //    std::cout << "Logical Part Name= "<<logicalpartid.str() << std::endl;
00101      
00102     //for table nominalPlacements
00103     bool reflection = false;
00104 
00105     DD3Vector x, y, z;
00106     epv.rotation().GetComponents(x, y, z);
00107     //Hep3Vector xv = epv.rotation().colX();
00108     //Hep3Vector yv = epv.rotation().colY();
00109     //Hep3Vector zv = epv.rotation().colZ();
00110     //if ( xv.cross(yv) * zv  < 0) {
00111     //            reflection = true;
00112     //          }
00113     if ( (1.0 + (x.Cross(y)).Dot(z)) <= tolerance ) {
00114       reflection = true;
00115     }
00116     std::vector<double> comps(9);
00117     epv.rotation().GetComponents(comps.begin(), comps.end());
00118     nominalPlacementsOS<< logicalpartid.str()<<","
00119                        << epv.translation().X() << "," 
00120                        << epv.translation().Y() << ","  
00121                        << epv.translation().Z()<< "," 
00122 //                     << epv.rotation().xx()<< "," 
00123 //                     << epv.rotation().xy()<< "," 
00124 //                     << epv.rotation().xz()<< "," 
00125 //                     << epv.rotation().yx()<< "," 
00126 //                     << epv.rotation().yy()<< "," 
00127 //                     << epv.rotation().yz()<< "," 
00128 //                     << epv.rotation().zx()<< "," 
00129 //                     << epv.rotation().zy()<< "," 
00130 //                     << epv.rotation().zz()<< "," 
00131                        << comps[0]<<","
00132                        << comps[1]<<","
00133                        << comps[2]<<","
00134                        << comps[3]<<","
00135                        << comps[4]<<","
00136                        << comps[5]<<","
00137                        << comps[6]<<","
00138                        << comps[7]<<","
00139                        << comps[8]<<","
00140                        << (int)reflection
00141                        <<std::endl;
00142 
00143 
00144     //for table DetectorParts
00145     detectorPartsOS <<epv.logicalPart().solid().toString() << ","
00146                     <<epv.logicalPart().material().toString() << ","
00147                     <<logicalpartid.str()
00148                     <<std::endl;
00149    
00150     //for table PosPartsGraph 
00151 
00152     pospartsGraphOS<<++pospartid<< ","<<epv.copyno()<< ","
00153     <<parent.str() << "," << child.str()<<std::endl;
00154 
00155 
00156     //for table logicalPartTypes
00157     //this table is special because the DPNAMEs are subset of Physicalpart_id
00158     //while we're looping all the Physicalpart_ids in epv, so we just 
00159     //keep the unique DPNAMEs into a vector and save them in looping epv.
00160     // if there's no lpname in lpname_vec
00161     //>> the OS file.
00162  
00163     slpname=epv.logicalPart().toString();
00164 
00165     std::vector<std::string>::iterator it = find(lpname_vec.begin(), lpname_vec.end(), slpname);    
00166    
00167     if (it==lpname_vec.end()){
00168       if(DDEnums::categoryName(epv.logicalPart().category())){     
00169         std::ostringstream category;
00170         category<<DDEnums::categoryName(epv.logicalPart().category());
00171         scategory=category.str();
00172         std::string unspec="unspecified";
00173         if(scategory==unspec){
00174           lpname_vec.push_back(slpname);
00175           logicalPartTypesOS <<slpname<< ","
00176                              <<"DETECTORPARTS"<<std::endl;
00177         }
00178         else{
00179           lpname_vec.push_back(slpname);
00180           logicalPartTypesOS <<slpname<< ","<<scategory<<std::endl;
00181 
00182         }
00183       }
00184       else{
00185         lpname_vec.push_back(slpname);
00186         logicalPartTypesOS <<slpname<< ","
00187                            <<"DETECTORPARTS"<<std::endl;
00188 
00189       }
00190     }
00191 
00192 
00193 
00194   } //finish looping through expanded view.
00195  
00196 
00197   physicalPartsTreeOS.close();
00198   logicalPartTypesOS.close();
00199   nominalPlacementsOS.close();
00200   detectorPartsOS.close();
00201   pospartsGraphOS.close();
00202 }
00203 
00204 //define this as a plug-in
00205 DEFINE_FWK_MODULE(PhysicalPartsTree);