CMS 3D CMS Logo

DDG4Builder.cc

Go to the documentation of this file.
00001 #include "DetectorDescription/Core/interface/DDSpecifics.h"
00002 
00003 #include "FWCore/Utilities/interface/Exception.h"
00004 
00005 #include "SimG4Core/Geometry/interface/DDG4Builder.h"
00006 #include "SimG4Core/Geometry/interface/DDG4SolidConverter.h"
00007 #include "SimG4Core/Geometry/interface/DDG4SensitiveConverter.h"
00008 #include "SimG4Core/Notification/interface/SimG4Exception.h"
00009 #include "DetectorDescription/Core/interface/DDCompactView.h"
00010 
00011 #include "G4VSolid.hh"
00012 #include "G4Box.hh"
00013 #include "G4Tubs.hh"
00014 #include "G4Cons.hh"
00015 #include "G4Trap.hh"
00016 #include "G4Material.hh"
00017 #include "G4LogicalVolume.hh"
00018 #include "G4VPhysicalVolume.hh"
00019 #include "G4PVPlacement.hh"
00020 #include "G4ReflectionFactory.hh"
00021 
00022 #include "G4UnitsTable.hh"
00023 
00024 #include <sstream>
00025 
00026 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00027 
00028 DDG4DispContainer * DDG4Builder::theVectorOfDDG4Dispatchables_ = 0;
00029 
00030 DDG4DispContainer * DDG4Builder::theVectorOfDDG4Dispatchables() { 
00031   return theVectorOfDDG4Dispatchables_; 
00032 }
00033 
00034 DDG4Builder::DDG4Builder(const DDCompactView* cpv, bool check) : 
00035   solidConverter_(new DDG4SolidConverter), compactView(cpv), check_(check) { 
00036   theVectorOfDDG4Dispatchables_ = new DDG4DispContainer(); 
00037 }
00038 
00039 DDG4Builder::~DDG4Builder() { 
00040   delete solidConverter_; 
00041 }
00042 
00043 G4LogicalVolume * DDG4Builder::convertLV(const DDLogicalPart & part) {
00044   LogDebug("SimG4CoreGeometry") << "DDG4Builder::convertLV(): DDLogicalPart = " << part;
00045   G4LogicalVolume * result = logs_[part];
00046   if (!result) {
00047     G4VSolid * s   = convertSolid(part.solid());
00048     G4Material * m = convertMaterial(part.material());
00049     result = new G4LogicalVolume(s,m,part.name().name());
00050     map_.insert(result,part);
00051     DDG4Dispatchable * disp = new DDG4Dispatchable(&part,result);       
00052     theVectorOfDDG4Dispatchables_->push_back(disp);
00053     LogDebug("SimG4CoreGeometry") << "DDG4Builder::convertLV(): new G4LogicalVolume " << part.name().name()
00054                                   << "\nDDG4Builder: newEvent: dd=" << part.ddname() << " g4=" << result->GetName();
00055     logs_[part] = result;  // DDD -> GEANT4  
00056   }
00057   return result;
00058 }
00059 
00060 G4VSolid * DDG4Builder::convertSolid(const DDSolid & solid) {
00061   G4VSolid * result = sols_[solid];
00062   if (!result) { 
00063     result = solidConverter_->convert(solid); sols_[solid] = result; 
00064   }
00065   return result;
00066 }
00067 
00068 G4Material * DDG4Builder::convertMaterial(const DDMaterial & material) {
00069   LogDebug("SimG4CoreGeometry") << "DDDetConstr::ConvertMaterial: material=" << material;
00070   G4Material * result = 0;
00071   if (material) {
00072     // only if it's a valid DDD-material
00073     if ((result = mats_[material])) {
00074       LogDebug("SimG4CoreGeometry") << "  is already converted"; 
00075       return result; }
00076   } else {
00077     // only if it's NOT a valid DDD-material
00078     edm::LogError("SimG4CoreGeometry") << "DDG4Builder::  material " << material.toString() << " is not valid (in the DDD sense!)";
00079     throw cms::Exception("SimG4CoreGeometry", " material is not valid from the Detector Description: " + material.toString()); 
00080   }    
00081   int c = 0;
00082   if ((c = material.noOfConstituents())) {
00083     // it's a composite material
00084     LogDebug("SimG4CoreGeometry") << "  creating a G4-composite material. c=" << c
00085                                   << " d=" << material.density()/g*mole ;
00086     result = new G4Material(material.name().name(),material.density(),c);
00087     for (int i=0 ; i<c; ++i) {
00088       // recursive building of constituents
00089       LogDebug("SimG4CoreGeometry") << "  adding the composite=" << material.name()
00090                                     << " fm=" << material.constituent(i).second;
00091       result->AddMaterial
00092         (convertMaterial(material.constituent(i).first),
00093          material.constituent(i).second);// fractionmass
00094     }
00095   } else {
00096     // it's an elementary material
00097     LogDebug("SimG4CoreGeometry") << "  building an elementary material"
00098                                   << " z=" << material.z()
00099                                   << " a=" << material.a()/g*mole
00100                                   << " d=" << material.density()/g*cm3 ;
00101     result = new G4Material
00102       (material.name().name(),material.z(),material.a(),material.density());
00103   }
00104   mats_[material] = result;
00105   return result;
00106 } 
00107 
00108 DDGeometryReturnType DDG4Builder::BuildGeometry() {
00109   G4ReflectionFactory * refFact = G4ReflectionFactory::Instance();
00110   refFact->SetScalePrecision(100.*refFact->GetScalePrecision());
00111 
00112   const graph_type & gra = compactView->graph();
00113   typedef graph_type::const_adj_iterator adjl_iterator;
00114   adjl_iterator git = gra.begin();
00115   adjl_iterator gend = gra.end();    
00116 
00117   graph_type::index_type i=0;
00118   for (; git != gend; ++git) {
00119     const DDLogicalPart & ddLP = gra.nodeData(git);
00120     if ( !(ddLP.isDefined().second) ) {
00121       edm::LogError("SimG4CoreGeometry") << "DDG4Builder::BuildGeometry() has encountered an undefined DDLogicalPart named " << ddLP.toString();
00122       throw cms::Exception("SimG4CoreGeometry", " DDG4Builder::BuildGeometry() has encountered an undefined DDLogicalPart named " + ddLP.toString());
00123     }
00124     G4LogicalVolume * g4LV = convertLV(ddLP);
00125     ++i;        
00126     if (git->size()) {
00127       // ask for children of ddLP  
00128       graph_type::edge_list::const_iterator cit  = git->begin();
00129       graph_type::edge_list::const_iterator cend = git->end();
00130       for (; cit != cend; ++cit) {
00131         // fetch specific data
00132         const DDLogicalPart & ddcurLP = gra.nodeData(cit->first);
00133         if ( !ddcurLP.isDefined().second ) {
00134           std::string err = " DDG4Builder::BuildGeometry() in processing \"children\" has ";
00135           err += "encountered an undefined DDLogicalPart named " + ddLP.toString();
00136           edm::LogError("SimG4CoreGeometry") << err;
00137           throw cms::Exception("SimG4CoreGeometry", err) ;
00138         }
00139         int offset = getInt("CopyNoOffset",ddcurLP);
00140         int tag = getInt("CopyNoTag",ddcurLP);                          
00141         DDRotationMatrix rm(gra.edgeData(cit->second)->rot());
00142         DD3Vector x, y, z;
00143         rm.GetComponents(x, y, z);
00144         if ((x.Cross(y)).Dot(z)<0)
00145           LogDebug("SimG4CoreGeometry") << ">>Reflection encountered: " << gra.edgeData(cit->second)->rot_ ;
00146         LogDebug("SimG4CoreGeometry") << ">>Placement d=" << gra.nodeData(cit->first).ddname() 
00147                                       << " m=" << ddLP.ddname() << " cp=" << gra.edgeData(cit->second)->copyno_
00148                                       << " r=" << gra.edgeData(cit->second)->rot_.ddname() ;          
00149         G4ThreeVector tempTran(gra.edgeData(cit->second)->trans_.X(), gra.edgeData(cit->second)->trans_.Y(), gra.edgeData(cit->second)->trans_.Z());
00150         G4Translate3D transl = tempTran;
00151         HepRep3x3 temp( x.X(), x.Y(), x.Z(), y.X(), y.Y(), y.Z(), z.X(), z.Y(), z.Z() ); //matrix representation
00152         HepRotation hr ( temp );
00153             
00154         // G3 convention of defining rot-matrices ...
00155         G4Transform3D trfrm  = transl * G4Rotate3D(hr.inverse());//.inverse();
00156 
00157         refFact->Place(trfrm, // transformation containing a possible reflection
00158                        gra.nodeData(cit->first).name().name(),
00159                        convertLV(gra.nodeData(cit->first)),             // daugther
00160                        g4LV,                                            // mother
00161                        false,                                           // 'ONLY'
00162                        gra.edgeData(cit->second)->copyno_+offset+tag,   // copy number
00163                        check_);
00164       } // iterate over children
00165     } // if (children)
00166   } // iterate over graph nodes  
00167     
00168   // Looking for in the G4ReflectionFactory secretly created reflected G4LogicalVolumes
00169   std::map<DDLogicalPart, G4LogicalVolume*>::const_iterator  ddg4_it = logs_.begin();
00170   for (; ddg4_it != logs_.end(); ++ddg4_it) {
00171     G4LogicalVolume * reflLogicalVolume = refFact->GetReflectedLV(ddg4_it->second);
00172     if (reflLogicalVolume) {
00173       DDLogicalPart ddlv = ddg4_it->first;
00174       map_.insert(reflLogicalVolume,ddlv);
00175       DDG4Dispatchable * disp = new DDG4Dispatchable(&(ddg4_it->first),reflLogicalVolume);
00176       theVectorOfDDG4Dispatchables_->push_back(disp);
00177       LogDebug("SimG4CoreGeometry") << "DDG4Builder: newEvent: dd=" 
00178                                     << ddlv.ddname() << " g4=" 
00179                                     << reflLogicalVolume->GetName();
00180     }  
00181   }
00182       
00183   G4LogicalVolume * world = logs_[compactView->root()];
00184 
00185   //
00186   //  needed for building sensitive detectors
00187   //
00188   DDG4SensitiveConverter conv_;
00189   SensitiveDetectorCatalog catalog = conv_.upDate(*theVectorOfDDG4Dispatchables_);
00190 
00191   return DDGeometryReturnType(world,map_,catalog);    
00192 }
00193 
00194 int DDG4Builder::getInt(const std::string & s, const DDLogicalPart & part)
00195 {
00196   DDValue val(s);
00197   std::vector<const DDsvalues_type *> result = part.specifics();
00198   std::vector<const DDsvalues_type *>::iterator it = result.begin();
00199   bool foundIt = false;
00200   for (; it != result.end(); ++it) {
00201     foundIt = DDfetch(*it,val);
00202     if (foundIt) break;
00203   }    
00204   if (foundIt) { 
00205     std::vector<double> temp = val.doubles();
00206     if (temp.size() != 1) {
00207       edm::LogError("SimG4CoreGeometry") << " DDG4Builder - ERROR: I need only 1 " << s ;
00208       throw SimG4Exception("DDG4Builder: Problem with Region tags - one and only one allowed");
00209     }      
00210     return int(temp[0]);
00211   }
00212   else return 0;
00213 }
00214 
00215 double DDG4Builder::getDouble(const std::string & s, 
00216                               const DDLogicalPart & part) {
00217   DDValue val(s);
00218   std::vector<const DDsvalues_type *> result = part.specifics();
00219   std::vector<const DDsvalues_type *>::iterator it = result.begin();
00220   bool foundIt = false;
00221   for (; it != result.end(); ++it) {
00222     foundIt = DDfetch(*it,val);
00223     if (foundIt) break;
00224   }    
00225   if (foundIt) { 
00226     std::vector<std::string> temp = val.strings();
00227     if (temp.size() != 1) {
00228       edm::LogError("SimG4CoreGeometry") << " DDG4Builder - ERROR: I need only 1 " << s ;
00229       throw SimG4Exception("DDG4Builder: Problem with Region tags - one and only one allowed");
00230     }
00231     double v;
00232     std::string unit;
00233     std::istringstream is(temp[0].c_str());
00234     is >> v >> unit;
00235     v  = v*G4UnitDefinition::GetValueOf(unit.substr(1,unit.size()));
00236     return v;
00237   }
00238   else return 0;
00239 }

Generated on Tue Jun 9 17:47:03 2009 for CMSSW by  doxygen 1.5.4