DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4GeometryScanner.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework include files
15 #include <DD4hep/Objects.h>
16 #include <DDG4/Defs.h>
18 
19 // Forward declarations
20 class G4LogicalVolume;
21 
23 namespace dd4hep {
24 
26  namespace sim {
27 
29 
36  protected:
38  class StepInfo {
39  public:
43  std::string path;
45  const G4LogicalVolume* volume;
46 
48  StepInfo(const Position& pre,
49  const Position& post,
50  const G4LogicalVolume* volume,
51  const std::string& path);
53  StepInfo(const StepInfo& c);
55  ~StepInfo() {}
57  StepInfo& operator=(const StepInfo& c);
58  };
59  typedef std::vector<StepInfo*> Steps;
61  double m_sumPath = 0;
62  public:
64  Geant4GeometryScanner(Geant4Context* context, const std::string& name);
66  virtual ~Geant4GeometryScanner();
68  virtual void operator()(const G4Step* step, G4SteppingManager* mgr);
70  virtual void begin(const G4Track* track);
72  virtual void end(const G4Track* track);
74  void beginEvent(const G4Event* event);
75  };
76  }
77 }
78 
79 //====================================================================
80 // AIDA Detector description implementation
81 //--------------------------------------------------------------------
82 //
83 // Author : M.Frank
84 //
85 //====================================================================
86 
87 // Framework include files
88 #include <DD4hep/InstanceCount.h>
89 #include <DD4hep/Printout.h>
91 #include <DDG4/Geant4StepHandler.h>
92 #include <DDG4/Geant4EventAction.h>
94 #include <CLHEP/Units/SystemOfUnits.h>
95 #include <G4LogicalVolume.hh>
96 #include <G4Material.hh>
97 #include <G4VSolid.hh>
98 
99 using namespace dd4hep::sim;
100 
101 #include <DDG4/Factories.h>
103 
106  const Position& postPos,
107  const G4LogicalVolume* vol,
108  const std::string& p)
109 : pre(prePos), post(postPos), path(p), volume(vol)
110 {
111 }
112 
115  : pre(c.pre), post(c.post), path(c.path), volume(c.volume)
116 {
117 }
118 
121  pre = c.pre;
122  post = c.post;
123  volume = c.volume;
124  return *this;
125 }
126 
129  : Geant4SteppingAction(ctxt,nam)
130 {
131  m_needsControl = true;
136 }
137 
141 }
142 
144 void Geant4GeometryScanner::operator()(const G4Step* step, G4SteppingManager*) {
145  Geant4StepHandler h(step);
146  Geant4TouchableHandler handler(step);
147  m_steps.emplace_back(new StepInfo(h.prePos(), h.postPos(), h.logvol(h.pre), handler.path()));
148 }
149 
151 void Geant4GeometryScanner::beginEvent(const G4Event* /* event */) {
152  for_each(m_steps.begin(),m_steps.end(),detail::DestroyObject<StepInfo*>());
153  m_steps.clear();
154  m_sumPath = 0;
155 }
156 
158 void Geant4GeometryScanner::begin(const G4Track* track) {
159  printP2("Starting tracking action for track ID=%d",track->GetTrackID());
160  for_each(m_steps.begin(),m_steps.end(),detail::DestroyObject<StepInfo*>());
161  m_steps.clear();
162  m_sumPath = 0;
163 }
164 
166 void Geant4GeometryScanner::end(const G4Track* track) {
167  if ( !m_steps.empty() ) {
168  constexpr const char* line = " +--------------------------------------------------------------------------------------------------------------------------------------------------\n";
169  constexpr const char* fmt = " | %5d %11.4f %9.3f (%7.2f,%7.2f,%7.2f) Path:\"/world%s\" Shape:%s Mat:%s\n";
170  const Position& start = m_steps[0]->pre;
171  const Position& stop = m_steps[m_steps.size()-1]->post;
172 
173  ::printf("%s + Geometry scan between: x_0 = (%7.2f,%7.2f,%7.2f) [cm] and x_1 = (%7.2f,%7.2f,%7.2f) [cm] TrackID:%d: \n%s",
174  line,start.X()/CLHEP::cm,start.Y()/CLHEP::cm,start.Z()/CLHEP::cm,
175  stop.X()/CLHEP::cm,stop.Y()/CLHEP::cm,stop.Z()/CLHEP::cm,
176  track->GetTrackID(),line);
177  ::printf(" | \\ Path \n");
178  ::printf(" | Num. \\ Thickness Length Endpoint Volume , Shape , Material\n");
179  ::printf(" | Layer \\ [cm] [cm] ( cm, cm, cm) \n");
180  ::printf("%s",line);
181  int count = 1;
182  for(Steps::const_iterator i=m_steps.begin(); i!=m_steps.end(); ++i, ++count) {
183  const G4LogicalVolume* logVol = (*i)->volume;
184  G4Material* material = logVol->GetMaterial();
185  G4VSolid* solid = logVol->GetSolid();
186  const Position& prePos = (*i)->pre;
187  const Position& postPos = (*i)->post;
188  Position direction = postPos - prePos;
189  double length = direction.R()/CLHEP::cm;
190  m_sumPath += length;
191  ::printf(fmt,count,
192  length, m_sumPath,
193  postPos.X()/CLHEP::cm,postPos.Y()/CLHEP::cm,postPos.Z()/CLHEP::cm,
194  (*i)->path.c_str(), typeName(typeid(*solid)).c_str(), material->GetName().c_str());
195  }
196  for_each(m_steps.begin(),m_steps.end(),detail::DestroyObject<StepInfo*>());
197  m_steps.clear();
198  }
199 }
dd4hep::sim::Geant4Action::printP2
void printP2(const char *fmt,...) const
Support for messages with variable output level using output level+2.
Definition: Geant4Action.cpp:188
dd4hep::sim::Geant4TouchableHandler
Helper class to ease the extraction of information from a G4Touchable object.
Definition: Geant4TouchableHandler.h:44
dd4hep::sim::Geant4Action::m_needsControl
bool m_needsControl
Default property: Flag to create control instance.
Definition: Geant4Action.h:123
Objects.h
dd4hep::sim::Geant4StepHandler::prePos
Position prePos() const
Returns the pre-step position.
Definition: Geant4StepHandler.h:84
dd4hep::sim::Geant4SteppingAction
Concrete implementation of the Geant4 stepping action sequence.
Definition: Geant4SteppingAction.h:40
dd4hep::sim::Geant4GeometryScanner::StepInfo::path
std::string path
Path to this volume.
Definition: Geant4GeometryScanner.cpp:43
dd4hep::sim::Geant4StepHandler::pre
G4StepPoint * pre
Definition: Geant4StepHandler.h:49
Geant4TrackingAction.h
dd4hep::sim::Geant4GeometryScanner::~Geant4GeometryScanner
virtual ~Geant4GeometryScanner()
Default destructor.
Definition: Geant4GeometryScanner.cpp:139
Geant4EventAction.h
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
dd4hep::sim::Geant4TrackingActionSequence::callAtBegin
void callAtBegin(Q *p, void(T::*f)(const G4Track *), CallbackSequence::Location where=CallbackSequence::END)
Register Pre-track action callback.
Definition: Geant4TrackingAction.h:150
Geant4StepHandler.h
DECLARE_GEANT4ACTION
#define DECLARE_GEANT4ACTION(name)
Plugin defintion to create Geant4Action objects.
Definition: Factories.h:210
dd4hep::sim::Geant4GeometryScanner::beginEvent
void beginEvent(const G4Event *event)
Registered callback on Begin-event.
Definition: Geant4GeometryScanner.cpp:151
Geant4SteppingAction.h
dd4hep::sim::Geant4GeometryScanner::end
virtual void end(const G4Track *track)
End-of-tracking callback.
Definition: Geant4GeometryScanner.cpp:166
dd4hep::sim::Geant4StepHandler
Helper class to ease the extraction of information from a G4Step object.
Definition: Geant4StepHandler.h:46
dd4hep::sim::Geant4Action::eventAction
Geant4EventActionSequence & eventAction() const
Access to the main event action sequence from the kernel object.
Definition: Geant4Action.cpp:283
dd4hep::sim::Geant4EventActionSequence::callAtBegin
void callAtBegin(Q *p, void(T::*f)(const G4Event *))
Register begin-of-event callback.
Definition: Geant4EventAction.h:152
dd4hep::sim::Geant4GeometryScanner::operator()
virtual void operator()(const G4Step *step, G4SteppingManager *mgr)
User stepping callback.
Definition: Geant4GeometryScanner.cpp:144
dd4hep::sim::Geant4GeometryScanner::StepInfo::~StepInfo
~StepInfo()
Default destructor.
Definition: Geant4GeometryScanner.cpp:55
dd4hep::sim::Geant4GeometryScanner::StepInfo::operator=
StepInfo & operator=(const StepInfo &c)
Assignment operator.
Definition: Geant4GeometryScanner.cpp:120
dd4hep::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
Geant4TouchableHandler.h
dd4hep::sim::Geant4GeometryScanner
Class to perform directional material scans using Geantinos.
Definition: Geant4GeometryScanner.cpp:35
dd4hep::sim::Geant4GeometryScanner::StepInfo::post
Position post
Definition: Geant4GeometryScanner.cpp:41
dd4hep::sim::Geant4GeometryScanner::Geant4GeometryScanner
Geant4GeometryScanner(Geant4Context *context, const std::string &name)
Standard constructor.
Definition: Geant4GeometryScanner.cpp:128
dd4hep::sim::Geant4Action::name
const std::string & name() const
Access name of the action.
Definition: Geant4Action.h:280
dd4hep::sim::Geant4GeometryScanner::begin
virtual void begin(const G4Track *track)
Begin-of-tracking callback.
Definition: Geant4GeometryScanner.cpp:158
dd4hep::sim::Geant4GeometryScanner::m_steps
Steps m_steps
Definition: Geant4GeometryScanner.cpp:60
dd4hep::sim::Geant4StepHandler::postPos
Position postPos() const
Returns the post-step position.
Definition: Geant4StepHandler.h:93
dd4hep::sim::Geant4GeometryScanner::m_sumPath
double m_sumPath
Definition: Geant4GeometryScanner.cpp:61
dd4hep::Position
ROOT::Math::XYZVector Position
Definition: Objects.h:81
Factories.h
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: Geant4Output2EDM4hep.cpp:49
dd4hep::sim::Geant4GeometryScanner::StepInfo::pre
Position pre
Pre-step and Post-step position.
Definition: Geant4GeometryScanner.cpp:41
dd4hep::sim::Geant4TouchableHandler::path
std::string path() const
Helper: Access the placement path of a Geant4 touchable object as a string.
Definition: Geant4TouchableHandler.cpp:58
dd4hep::sim::Geant4GeometryScanner::StepInfo::volume
const G4LogicalVolume * volume
Reference to the logical volue.
Definition: Geant4GeometryScanner.cpp:45
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::sim::Geant4GeometryScanner::StepInfo::StepInfo
StepInfo(const Position &pre, const Position &post, const G4LogicalVolume *volume, const std::string &path)
Initializing constructor.
Definition: Geant4GeometryScanner.cpp:105
dd4hep::sim::Geant4GeometryScanner::StepInfo
Structure to hold the information of one simulation step.
Definition: Geant4GeometryScanner.cpp:38
InstanceCount.h
Defs.h
dd4hep::sim::Geant4GeometryScanner::Steps
std::vector< StepInfo * > Steps
Definition: Geant4GeometryScanner.cpp:59
Printout.h
dd4hep::sim::Geant4Context
Generic context to extend user, run and event information.
Definition: Geant4Context.h:201
dd4hep::sim::Geant4StepHandler::logvol
G4LogicalVolume * logvol(const G4StepPoint *p) const
Definition: Geant4StepHandler.h:160
dd4hep::sim::Geant4Action::context
Geant4Context * context() const
Access the context.
Definition: Geant4Action.h:270
dd4hep::sim::Geant4Action::trackingAction
Geant4TrackingActionSequence & trackingAction() const
Access to the main tracking action sequence from the kernel object.
Definition: Geant4Action.cpp:293
dd4hep::sim::Geant4TrackingActionSequence::callAtEnd
void callAtEnd(Q *p, void(T::*f)(const G4Track *), CallbackSequence::Location where=CallbackSequence::END)
Register Post-track action callback.
Definition: Geant4TrackingAction.h:156