DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4SurfaceTest.cpp
Go to the documentation of this file.
1 // AIDA Detector description implementation
2 //--------------------------------------------------------------------------
3 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
4 // All rights reserved.
5 //
6 // For the licensing terms see $DD4hepINSTALL/LICENSE.
7 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
8 //
9 // \author Markus Frank
10 // \date 2015-10-14
11 //
12 //==========================================================================
13 #ifndef DD4HEP_DDG4_GEANT4SURFACETEST_H
14 #define DD4HEP_DDG4_GEANT4SURFACETEST_H
15 
16 // Framework include files
17 //#include "Geant4SurfaceTest.h"
18 #include <DDG4/Geant4EventAction.h>
19 
21 namespace dd4hep {
22 
24  namespace sim {
25 
29  public:
31  Geant4SurfaceTest(Geant4Context* context, const std::string& nam);
33  virtual ~Geant4SurfaceTest( );
34 
36  virtual void begin(const G4Event* event);
38  virtual void end(const G4Event* event);
39 
40  protected:
41 
42  private:
43 
44  };
45  } // End namespace sim
46 } // End namespace dd4hep
47 
48 #endif // DD4HEP_DDG4_GEANT4SURFACETEST_H
49 
50 // Framework include files
51 #include <DD4hep/Detector.h>
52 #include <DD4hep/DD4hepUnits.h>
53 #include <DD4hep/InstanceCount.h>
54 
55 #include <DDG4/Factories.h>
56 #include <DDG4/Geant4Data.h>
57 #include <DDG4/Geant4Context.h>
60 
61 #include <DDRec/Surface.h>
62 #include <DDRec/DetectorSurfaces.h>
63 #include <DDRec/SurfaceManager.h>
64 #include <DDRec/SurfaceHelper.h>
65 
66 // Geant 4 includes
67 #include <G4VHitsCollection.hh>
68 #include <G4HCofThisEvent.hh>
69 #include <G4Event.hh>
70 
71 // C/C++ include files
72 #include <stdexcept>
73 #include <map>
74 #include <sstream>
75 
76 using namespace dd4hep::DDRec;
77 using namespace dd4hep::detail;
78 using namespace dd4hep::sim;
79 
81 
82 Geant4SurfaceTest::Geant4SurfaceTest(Geant4Context* ctxt, const std::string& nam)
84 : Geant4EventAction(ctxt, nam)
85 {
87 }
88 
92 }
93 
95 void Geant4SurfaceTest::begin(const G4Event* event) {
96  if ( event ) {
97  }
98 }
99 
101 void Geant4SurfaceTest::end(const G4Event* evt) {
102  stringstream sst;
103  Detector& description = context()->detectorDescription();
104  SurfaceManager& surfMan = *description.extension< SurfaceManager >() ;
105  const SurfaceMap& surfMap = *surfMan.map( "world" ) ;
106  G4HCofThisEvent* hce = evt->GetHCofThisEvent();
107 
108  if ( !hce ) {
109  error("Event:%d: No Geant4 event record (HCE) found.",evt->GetEventID());
110  return;
111  }
112 
113  for(int j=0, nCol=hce->GetNumberOfCollections(); j<nCol; ++j) {
114  G4VHitsCollection* hc = hce->GetHC(j);
115  Geant4HitCollection* coll = dynamic_cast<Geant4HitCollection*>(hc);
116 
117  if ( !(coll && coll->type().type == typeid(Geant4Tracker::Hit)) ) {
118  continue;
119  }
120 
122  if ( !sd.isValid() ) {
123  error("Event:%d: Collection:%s Invalid sensitive detector.",
124  evt->GetEventID(),hc->GetName().c_str());
125  continue;
126  }
127  IDDescriptor idDesc = sd.readout().idSpec();
128  const string idStr = idDesc.fieldDescription();
129  BitField64 idDecoder(idStr);
130  info("Event: %d [%s: %d hits] Desc:%s",
131  evt->GetEventID(),hc->GetName().c_str(),coll->GetSize(),idStr.c_str());
132  for(size_t i=0, nHit=coll->GetSize(); i<nHit; ++i) {
133  Geant4Tracker::Hit* h = coll->hit(i);
134  const Position& pos = h->position;
135  int trackID = h->truth.trackID;
136  idDecoder.setValue( h->cellID ) ;
137  SurfaceMap::const_iterator si = surfMap.find(h->cellID);
138  ISurface* surf = (si != surfMap.end() ? si->second : 0);
139 
140  // ===== test that we have a surface with the correct ID for every hit ======================
141  if ( !surf ) {
142  error("FAILED: Hit:%ld Track:%d No surface found cell id: %016llX",i,trackID,h->cellID);
143  continue;
144  }
145  info("PASSED: Hit:%ld Track:%d Surface found for cell: %016llX",i,trackID,h->cellID);
146 
147  Vector3D hit_point(pos.x()*dd4hep::mm,pos.y()*dd4hep::mm,pos.z()*dd4hep::mm);
148  bool isInside = surf->insideBounds(hit_point);
149  string flag = "";
150  if ( h->flag & Geant4Tracker::Hit::HIT_SECONDARY_TRACK ) flag += " TRK:SECONDARY";
151  if ( h->flag & Geant4Tracker::Hit::HIT_KILLED_TRACK ) flag += " TRK:KILLED";
152  if ( h->flag & Geant4Tracker::Hit::HIT_NEW_TRACK ) flag += " NEW_TRACK";
153  if ( h->flag & Geant4Tracker::Hit::HIT_STARTED_OUTSIDE ) flag += " START:OUTSIDE";
154  if ( h->flag & Geant4Tracker::Hit::HIT_STARTED_INSIDE ) flag += " START:INSIDE";
155  if ( h->flag & Geant4Tracker::Hit::HIT_STARTED_SURFACE ) flag += " START:SURFACE";
156  if ( h->flag & Geant4Tracker::Hit::HIT_ENDED_OUTSIDE ) flag += " END:OUTSIDE";
157  if ( h->flag & Geant4Tracker::Hit::HIT_ENDED_INSIDE ) flag += " END:INSIDE";
158  if ( h->flag & Geant4Tracker::Hit::HIT_ENDED_SURFACE ) flag += " END:SURFACE";
159 
160  sst.str("");
161  if ( isInside ) {
162  sst << hc->GetName() << " " << "PASSED: Track:" << trackID << " Hit:" << i
163  //<< " Point " << hit_point
164  << " is ON SURFACE. "
165  << "(FLAG:" << h->flag << flag << ").";
166  print(sst.str().c_str());
167  }
168  else {
169  double dist = surf->distance(hit_point)/dd4hep::mm;
170  sst.str("");
171  sst << hc->GetName() << " FAILED: Track:" << trackID << " Hit:" << i
172  << " Point: " << hit_point;
173  error(sst.str().c_str());
174  //sst.str("");
175  //sst << -> Surface:" << *surf;
176  //error(sst.str().c_str());
177  sst.str("");
178  sst << " -> Hit is NOT ON SURFACE (Flag:" << h->flag << flag << ")"
179  << " Distance to surface:" << dist << " mm.";
180  error(sst.str().c_str());
181  // No need for further tests, if this one failed....
182  continue;
183  }
184  // ====== Test to show that moved hit points are outside their surface
185  hit_point = hit_point + 1e-3 * surf->normal() ;
186  sst.str("") ;
187  sst << "Track:" << trackID << " Hit:" << i << " Moved:" << hit_point;
188  if ( (isInside=surf->insideBounds(hit_point)) ) {
189  error("FAILED: %s is ON SURFACE (SHOULD NOT BE)",sst.str().c_str());
190  continue;
191  }
192  info("PASSED: %s is NOT ON SURFACE (SHOULD NOT BE)",sst.str().c_str());
193  }
194  }
195 }
dd4hep::sim::Geant4Tracker::Hit::position
Position position
Hit position.
Definition: Geant4Data.h:268
dd4hep::sim::Geant4HitData::HIT_ENDED_OUTSIDE
@ HIT_ENDED_OUTSIDE
Definition: Geant4Data.h:121
dd4hep::sim::Geant4HitData::flag
long flag
User flag to classify hits.
Definition: Geant4Data.h:126
dd4hep::sim::Geant4HitCollection::GetSize
virtual size_t GetSize() const override
Access the collection size.
Definition: Geant4HitCollection.h:321
Geant4HitCollection.h
dd4hep::sim::Geant4HitData::HIT_ENDED_SURFACE
@ HIT_ENDED_SURFACE
Definition: Geant4Data.h:120
SurfaceManager.h
dd4hep::sim::Geant4Context::detectorDescription
Detector & detectorDescription() const
Access to detector description.
Definition: Geant4Context.cpp:91
dd4hep::SensitiveDetector
Handle class to hold the information of a sensitive detector.
Definition: DetElement.h:44
Detector.h
dd4hep::DDSegmentation::BitField64
A bit field of 64bits that allows convenient declaration.
Definition: BitField64.h:110
dd4hep::sim::Geant4HitData::HIT_NEW_TRACK
@ HIT_NEW_TRACK
Definition: Geant4Data.h:115
Geant4SensDetAction.h
dd4hep::sim::Geant4HitCollection
Generic hit container class using Geant4HitWrapper objects.
Definition: Geant4HitCollection.h:201
Geant4EventAction.h
dd4hep::sim::Geant4HitData::HIT_KILLED_TRACK
@ HIT_KILLED_TRACK
Definition: Geant4Data.h:113
dd4hep::Handle::isValid
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:128
dd4hep::sim::Geant4HitCollection::hit
Geant4HitWrapper & hit(size_t which)
Access the hit wrapper.
Definition: Geant4HitCollection.h:325
G4VHitsCollection
Class of the Geant4 toolkit. See http://www-geant4.kek.jp/Reference.
Definition: Geant4Classes.h:47
dd4hep::rec::SurfaceMap
std::multimap< unsigned long, ISurface * > SurfaceMap
typedef for surface maps, keyed by the cellID
Definition: SurfaceManager.h:25
SurfaceHelper.h
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
Geant4Data.h
dd4hep::sim::Geant4SurfaceTest::Geant4SurfaceTest
Geant4SurfaceTest(Geant4Context *context, const std::string &nam)
Standard constructor.
Definition: Geant4SurfaceTest.cpp:83
DECLARE_GEANT4ACTION
#define DECLARE_GEANT4ACTION(name)
Plugin defintion to create Geant4Action objects.
Definition: Factories.h:210
dd4hep::sim::Geant4HitData::HIT_SECONDARY_TRACK
@ HIT_SECONDARY_TRACK
Definition: Geant4Data.h:114
dd4hep::sim::Geant4HitData::cellID
long long int cellID
cellID
Definition: Geant4Data.h:124
SurfaceManager
Plugin that creates a SurfaceManager object and attaches it to description as a user extension object...
dd4hep::DDSegmentation::BitField64::setValue
void setValue(CellID value)
Definition: BitField64.h:153
dd4hep::sim::Geant4SurfaceTest::begin
virtual void begin(const G4Event *event)
Begin-of-event callback.
Definition: Geant4SurfaceTest.cpp:95
dd4hep::sim::Geant4Action::info
void info(const char *fmt,...) const
Support of info messages.
Definition: Geant4Action.cpp:215
Surface.h
dd4hep::sim::Geant4EventAction
Concrete basic implementation of the Geant4 event action.
Definition: Geant4EventAction.h:53
dd4hep::sim::Geant4HitData::HIT_ENDED_INSIDE
@ HIT_ENDED_INSIDE
Definition: Geant4Data.h:119
dd4hep::sim::Geant4HitCollection::sensitive
Geant4Sensitive * sensitive() const
Access the sensitive detector.
Definition: Geant4HitCollection.h:313
dd4hep::sim::Geant4Action::error
void error(const char *fmt,...) const
Support of error messages.
Definition: Geant4Action.cpp:231
dd4hep::detail
DD4hep internal namespace.
Definition: Alignments.h:32
dd4hep::sim::Geant4Tracker::Hit
DDG4 tracker hit class used by the generic DDG4 tracker sensitive detector.
Definition: Geant4Data.h:263
dd4hep::sim::Geant4Sensitive::sensitiveDetector
SensitiveDetector sensitiveDetector() const
Access the dd4hep sensitive detector.
Definition: Geant4SensDetAction.h:198
dd4hep::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
DetectorSurfaces.h
dd4hep::sim::Geant4SurfaceTest::end
virtual void end(const G4Event *event)
End-of-event callback.
Definition: Geant4SurfaceTest.cpp:101
dd4hep::sim::Geant4HitData::HIT_STARTED_OUTSIDE
@ HIT_STARTED_OUTSIDE
Definition: Geant4Data.h:118
dd4hep::sim::Geant4Tracker::Hit::truth
Contribution truth
Monte Carlo / Geant4 information.
Definition: Geant4Data.h:276
dd4hep::sim::IDDescriptor
IDDescriptor IDDescriptor
Definition: LCIOConversions.cpp:69
dd4hep::sim::Geant4HitCollection::type
const ComponentCast & type() const
Type information of the object stored.
Definition: Geant4HitCollection.cpp:87
dd4hep::sim::Geant4Action::print
void print(const char *fmt,...) const
Support for messages with variable output level using output level.
Definition: Geant4Action.cpp:144
dd4hep::sim::Geant4HitData::MonteCarloContrib::trackID
int trackID
Geant 4 Track identifier.
Definition: Geant4Data.h:141
dd4hep::sim::Geant4SurfaceTest::~Geant4SurfaceTest
virtual ~Geant4SurfaceTest()
Destructor.
Definition: Geant4SurfaceTest.cpp:90
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::Geant4SurfaceTest
Definition: Geant4SurfaceTest.cpp:28
dist
double dist(const Position &p0, const Position &p1)
Definition: test_cellid_position_converter.cpp:45
dd4hep::IDDescriptor::fieldDescription
std::string fieldDescription() const
The string description of all fields from the BitField.
Definition: IDDescriptor.cpp:67
dd4hep::Detector::extension
IFACE * extension(bool alert=true) const
Access extension element by the type.
Definition: Detector.h:342
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::SensitiveDetector::readout
Readout readout() const
Access readout structure of the sensitive detector.
Definition: DetElement.cpp:420
dd4hep::sim::Geant4HitData::HIT_STARTED_INSIDE
@ HIT_STARTED_INSIDE
Definition: Geant4Data.h:116
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
dd4hep::Readout::idSpec
IDDescriptor idSpec() const
Access IDDescription structure.
Definition: Readout.cpp:112
InstanceCount.h
dd4hep::sim::Geant4HitData::HIT_STARTED_SURFACE
@ HIT_STARTED_SURFACE
Definition: Geant4Data.h:117
DD4hepUnits.h
Geant4Context.h
dd4hep::sim::Geant4Context
Generic context to extend user, run and event information.
Definition: Geant4Context.h:201
dd4hep::sim::Geant4Action::context
Geant4Context * context() const
Access the context.
Definition: Geant4Action.h:270