Print

Print


Commit in lcdd/include on MAIN
StoreInspector.hh+373added 1.1
added class and other resources for printing out information from all the Geant4 and LCDD object stores (lvolume, solids, pvolume, etc.)

lcdd/include
StoreInspector.hh added at 1.1
diff -N StoreInspector.hh
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ StoreInspector.hh	9 Dec 2005 01:53:14 -0000	1.1
@@ -0,0 +1,373 @@
+// $Header: /cvs/lcd/lcdd/include/StoreInspector.hh,v 1.1 2005/12/09 01:53:14 jeremy Exp $
+#ifndef lcdd_StoreInspector_hh
+#define lcdd_StoreInspector_hh 1
+
+// geant4
+#include "G4UIcmdWithAString.hh"
+#include "G4UImessenger.hh"
+#include "G4UIdirectory.hh"
+
+// std
+#include <string>
+#include <vector>
+#include <map>
+#include <ostream>
+#include <cassert>
+#include <cmath>
+
+/**
+ * StoreInspector is a templated class for printing the contents
+ * of a vector or map-based object store using a G4UIcommand.
+ */
+template < class Object, class ObjectContainer >
+class StoreInspector : public G4UImessenger
+{
+public:
+
+  typedef typename ObjectContainer::iterator iterator;
+  typedef typename ObjectContainer::const_iterator const_iterator;
+
+public:
+
+  /** ctor */
+  StoreInspector(const std::string& name, const ObjectContainer* container)
+    : G4UImessenger()
+  {
+    assert(container != 0 );
+    _name = name;
+    _container = container;
+    defineCommands();
+  }
+
+  /* Overloaded find for vector. */
+  Object* findByName(const std::string& name, const std::vector<Object*>* objVec)
+  {
+    Object* obj = 0;
+    for ( typename std::vector<Object*>::const_iterator it = objVec->begin();
+	  it != objVec->end();
+	  it++ ) {
+      if ( sameName( *it, name ) ) {
+	obj = (*it);
+	break;
+      }
+    }
+    return obj;
+  }
+
+  /* Overloaded find for map. */
+  Object* findByName(const std::string& name, const std::map<std::string, Object*>* objMap)
+  {
+    std::map<std::string, Object*>* objMap_nconst
+      = const_cast< std::map< std::string, Object* >* > (objMap);
+    return (*objMap_nconst)[name];
+  }
+
+  /* Simple name comparison returning true if the strings match. */
+  bool sameName(Object* obj, const std::string& name)
+  {
+    return ( ((std::string)obj->GetName()) == name );
+  }
+
+  /* Find an object by name. */
+  Object* find(const std::string& name)
+  {
+    return findByName(name, const_cast< const ObjectContainer* > ( _container ) );
+  }
+
+  /* Object from iterator for maps. */
+  Object* getObject( typename std::map<std::string, Object*>::const_iterator it )
+  {
+    return it->second;
+  }
+
+  /* Object from iterator for vectors. */
+  Object* getObject( typename std::vector<Object*>::const_iterator it )
+  {
+    return (*it);
+  }
+
+  /* Name from iterator for maps. */
+  const std::string& getObjectName( typename std::map<std::string, Object*>::const_iterator it )
+  {
+    return it->first;
+  }
+
+  /* Name from iterator for vectors. */
+  const std::string& getObjectName( typename std::vector<Object*>::const_iterator it )
+  {
+    return (*it)->GetName();
+  }
+
+  /**
+   * Print out the objects in the store using the stream.
+   */
+    std::ostream& printStore(std::ostream& os)
+    {
+      os << "********* " + _name + " store ********" << std::endl << std::endl;
+      for ( const_iterator it = _container->begin();
+	    it != _container->end();
+	    it++ ) {
+	printObject( os, getObjectName( it ), getObject( it ) );
+	os << "-------------------------------------" << std::endl;
+      }
+      return os;
+      os << "*************************************" << std::endl << std::endl;
+    }
+
+  /**
+   * Print a single object's name and data using the stream operator.
+   */
+  std::ostream& printObject(std::ostream& os, const std::string& name, Object* obj)
+  {
+    os << "name: " << name << std::endl;
+    os << (*obj) << std::endl;
+    return os;
+  }
+
+  /* Defines G4UIcommand to be used with this inspector. */
+  void defineCommands()
+  {
+    _dir = new G4UIdirectory(std::string("/stores/" + _name).c_str());
+    _dir->SetGuidance( std::string("Commands for inspecting the " + _name + " store.").c_str() );
+
+    _printCmd = new G4UIcmdWithAString(std::string("/stores/"+_name+"/print").c_str(), this);
+    _printCmd->SetGuidance( std::string("Print single " +_name+ " object or the entire store.").c_str());
+    _printCmd->SetParameterName("name", true);
+    _printCmd->SetDefaultValue("");
+    _printCmd->AvailableForStates(G4State_Idle);
+  }
+
+  /**
+   * Hook for the G4UIcommand 'print' on this inspector.
+   */
+  virtual void SetNewValue(G4UIcommand*, G4String newVals)
+  {
+    std::string name = (std::string)newVals;
+    if ( name != std::string("") ) {
+      Object* obj = find(name);
+      if ( obj != 0 ) {
+	printObject( std::cout, name, obj );
+      }
+      else {
+	std::cerr << "ERROR: Object named " + name + " was not found in the " + _name + " store!" << std::endl;
+      }
+    }
+    else {
+      printStore( std::cout );
+    }
+  }
+
+private:
+  std::string _name;
+  const ObjectContainer* _container;
+  G4UIcmdWithAString* _printCmd;
+  G4UIdirectory* _dir;
+};
+
+// FIXME: None of these types/objects should be declared globally.
+
+#include "LCDDProcessor.hh"
+
+/* material */
+#include "G4MaterialTable.hh"
+
+typedef StoreInspector< G4Material, G4MaterialTable > G4MaterialStoreInspector;
+G4MaterialStoreInspector materialInspector( "material", G4Material::GetMaterialTable() );
+
+/* solid */
+#include "G4VSolid.hh"
+#include "G4SolidStore.hh"
+
+typedef StoreInspector< G4VSolid, G4SolidStore > G4SolidStoreInspector;
+G4SolidStoreInspector solidInspector( "solid", G4SolidStore::GetInstance() );
+
+/* limit set */
+#include "G4LimitSet.hh"
+
+std::ostream& operator<<(std::ostream& os, G4LimitSet&)
+{
+  return os;
+}
+
+typedef StoreInspector< G4LimitSet, LCDDProcessor::LimitSets > G4LimitSetInspector;
+G4LimitSetInspector limitsetInspector( "limitset", LCDDProcessor::instance()->getLimitSetStore() );
+
+/* lvolume */
+#include "G4LogicalVolume.hh"
+#include "G4LogicalVolumeStore.hh"
+#include "G4UserLimits.hh"
+
+/* G4LogicalVolume stream operator */
+std::ostream& operator<<(std::ostream &os, G4LogicalVolume &lv)
+{
+  os << "solid: " << lv.GetSolid()->GetName() << std::endl;
+  os << "material: " << lv.GetMaterial()->GetName() << std::endl;
+  os << "# daughters: " << lv.GetNoDaughters() << std::endl;
+
+  G4UserLimits* lim = lv.GetUserLimits();
+  os << "limits: ";
+  if ( 0 != lim ) {
+    os << lim->GetType();
+  }
+  else {
+    os << "NONE";
+  }
+  os << std::endl;
+
+  G4VSensitiveDetector* sd = lv.GetSensitiveDetector();
+  os << "SD: ";
+  if ( 0 != sd ) {
+    os << sd->GetName();
+  }
+  else {
+    os << "NONE";
+  }
+  os << std::endl;
+
+  G4Region* reg = lv.GetRegion();
+  os << "region: ";
+  if ( 0 != reg ) {
+    os << reg->GetName();
+  }
+  else {
+    os << "NONE";
+  }
+  os << std::endl;
+
+  const G4VisAttributes* vis = lv.GetVisAttributes();
+  os << "visattributes: ";
+  if ( 0 != vis ) {
+    os << "yes";
+  }
+  else {
+    os << "NONE";
+  }
+  os << std::endl;
+
+  return os;
+}
+
+typedef StoreInspector< G4LogicalVolume, G4LogicalVolumeStore > G4LogicalVolumeStoreInspector;
+G4LogicalVolumeStoreInspector lvolumeInspector( "lvolume", G4LogicalVolumeStore::GetInstance() );
+
+/* pvolume */
+#include "G4VPhysicalVolume.hh"
+#include "G4PhysicalVolumeStore.hh"
+
+std::ostream& operator<<(std::ostream &os, G4VPhysicalVolume &pv)
+{
+  os << "lvolume: " << pv.GetLogicalVolume()->GetName() << std::endl;
+  os << "pos: " << pv.GetTranslation() << std::endl;
+  const G4RotationMatrix* rot = pv.GetRotation();
+  os << "rot: ";
+  if ( 0 != rot ) {
+    rot->print(os);
+  }
+  else {
+    os << "NONE" << std::endl;
+  }
+  os << "copyNo: " << pv.GetCopyNo() << std::endl;
+  os << "# copies: " << pv.GetMultiplicity() << std::endl;
+  return os;
+}
+
+typedef StoreInspector< G4VPhysicalVolume, G4PhysicalVolumeStore > G4PhysicalVolumeStoreInspector;
+G4PhysicalVolumeStoreInspector pvolumeInspector( "pvolume", G4PhysicalVolumeStore::GetInstance() );
+
+/* SD */
+#include "G4SensitiveDetector.hh" // use LCDD's concrete SD class
+
+std::ostream& operator<<(std::ostream& os, G4SensitiveDetector& sd)
+{
+  os << "fullPath: " << sd.GetFullPathName() << std::endl;
+  os << "HC: " << sd.getHCName() << std::endl;
+  os << "HCID: " << sd.getHCID() << std::endl;
+  os << "verbose: " << sd.getVerbose() << std::endl;
+  os << "ecut: " << sd.getEcut() << std::endl;
+  os << "hasIdSpec: " << sd.hasIdSpec() << std::endl;
+  os << "isEndcap: " << sd.getEndcapFlag() << std::endl;
+  os << "isActive: " << sd.isActive() << std::endl;
+  return os;
+}
+
+typedef StoreInspector< G4SensitiveDetector, LCDDProcessor::SensitiveDetectors > G4SensitiveDetectorInspector;
+G4SensitiveDetectorInspector sdInspector( "sd", LCDDProcessor::instance()->getSensitiveDetectorStore() );
+
+/* region */
+#include "G4Region.hh"
+#include "G4RegionStore.hh"
+#include "G4ProductionCuts.hh"
+#include "G4VUserRegionInformation.hh"
+#include "G4UserRegionInformation.hh"
+
+/* G4Region stream operator */
+std::ostream& operator<<(std::ostream &os, G4Region &reg)
+{
+  os << "prod cut: " << reg.GetProductionCuts()->GetProductionCut(0) << std::endl;
+  G4UserRegionInformation* regInfo
+    = static_cast<G4UserRegionInformation*> ( reg.GetUserInformation() );
+  if ( 0 != regInfo ) {
+    os << "store secondaries: " << regInfo->getStoreSecondaries() << std::endl;
+    os << "energy threshold: " << regInfo->getThreshold() << std::endl;
+  }
+  else {
+    os << "NO G4UserRegionInformation" << std::endl;
+  }
+  return os;
+}
+
+typedef StoreInspector< G4Region, G4RegionStore > G4RegionStoreInspector;
+G4RegionStoreInspector regionInspector( "region", G4RegionStore::GetInstance() );
+
+/* mag field */
+#include "G4MagneticField.hh"
+
+std::ostream& operator<<(std::ostream& os, G4MagneticField&)
+{
+  /* Generic fields have no displayable data attributes! */
+  return os;
+}
+
+typedef StoreInspector< G4MagneticField, LCDDProcessor::MagneticFields > G4MagneticFieldInspector;
+G4MagneticFieldInspector fieldInspector( "field", LCDDProcessor::instance()->getMagneticFieldStore() );
+
+/* vis attrib */
+#include "G4VisAttributes.hh"
+
+/* G4VisAttributes stream operator */
+std::ostream& operator<<(std::ostream& os, G4VisAttributes &vis)
+{
+  const G4Color& color = vis.GetColor();
+  os << "RGB: "
+     << color.GetRed() << " "
+     << color.GetGreen() << " "
+     << color.GetBlue() << std::endl;
+  os << "Alpha: " << color.GetAlpha() << std::endl;
+  os << "visible: " << vis.IsVisible() << std::endl;
+  os << "show daughters: " << !vis.IsDaughtersInvisible() << std::endl;
+
+  os << "forced drawing style: ";
+  if ( vis.IsForceDrawingStyle() ) {
+    G4VisAttributes::ForcedDrawingStyle force = vis.GetForcedDrawingStyle();
+    if ( force == G4VisAttributes::wireframe ) {
+      os << "wireframe";
+    }
+    else if ( force == G4VisAttributes::solid ) {
+      os << "solid";
+    }
+  }
+  else {
+    os << "NONE";
+  }
+  os << std::endl;
+
+  return os;
+}
+
+typedef StoreInspector< G4VisAttributes, LCDDProcessor::VisAttributes > G4VisAttributeInspector;
+G4VisAttributeInspector visInspector( "visattributes", LCDDProcessor::instance()->getVisAttributesStore() );
+
+/* defines, positions, rotations */
+// FIXME: need accessible maps from GDML???
+
+#endif
CVSspam 0.2.8