ATLAS Offline Software
Loading...
Searching...
No Matches
G4UA::MaterialStepRecorder Class Reference

#include <MaterialStepRecorder.h>

Inheritance diagram for G4UA::MaterialStepRecorder:
Collaboration diagram for G4UA::MaterialStepRecorder:

Public Member Functions

 MaterialStepRecorder ()
virtual void BeginOfEventAction (const G4Event *) override
virtual void EndOfEventAction (const G4Event *) override
virtual void BeginOfRunAction (const G4Run *) override
virtual void UserSteppingAction (const G4Step *) override
bool msgLvl (const MSG::Level lvl) const
 Test the output level.
MsgStream & msg () const
 The standard message stream.
MsgStream & msg (const MSG::Level lvl) const
 The standard message stream.
void setLevel (MSG::Level lvl)
 Change the current logging level.

Private Member Functions

void initMessaging () const
 Initialize our message level and MessageSvc.

Private Attributes

ServiceHandle< StoreGateSvcm_evtStore
 Pointer to StoreGate (event store by default)
Trk::MaterialStepCollectionm_matStepCollection
std::string m_matStepCollectionName
bool m_recordComposition
size_t m_totalSteps
size_t m_eventID
Trk::ElementTablem_elementTable
std::string m_elementTableName
Trk::ElementTablem_runElementTable
std::string m_nm
 Message source name.
boost::thread_specific_ptr< MsgStream > m_msg_tls
 MsgStream instance (a std::cout like with print-out levels)
std::atomic< IMessageSvc * > m_imsg { nullptr }
 MessageSvc pointer.
std::atomic< MSG::Level > m_lvl { MSG::NIL }
 Current logging level.
std::atomic_flag m_initialized ATLAS_THREAD_SAFE = ATOMIC_FLAG_INIT
 Messaging initialized (initMessaging)

Detailed Description

Definition at line 38 of file MaterialStepRecorder.h.

Constructor & Destructor Documentation

◆ MaterialStepRecorder()

MaterialStepRecorder::MaterialStepRecorder ( )

Definition at line 33 of file MaterialStepRecorder.cxx.

33 :
34 AthMessaging(Gaudi::svcLocator()->service< IMessageSvc >( "MessageSvc" ),"MaterialStepRecorder"),
35 m_evtStore("StoreGateSvc/StoreGateSvc","MaterialStepRecorder"), //FIXME name should be passed in via a Config struct rather than hardcoded.
36 m_matStepCollection(nullptr),
37 m_matStepCollectionName("MaterialStepRecords"), //FIXME should be passed in via a Config struct rather than hardcoded.
38 m_recordComposition(true), //FIXME should be passed in via a Config struct rather than hardcoded.
39 m_totalSteps(0),
40 m_eventID(0),
41 m_elementTable(nullptr),
42 m_elementTableName("ElementTable"),
43 m_runElementTable(nullptr)
44 {
45 }
AthMessaging()
Default constructor:
Trk::ElementTable * m_runElementTable
Trk::MaterialStepCollection * m_matStepCollection
Trk::ElementTable * m_elementTable
ServiceHandle< StoreGateSvc > m_evtStore
Pointer to StoreGate (event store by default)

Member Function Documentation

◆ BeginOfEventAction()

void MaterialStepRecorder::BeginOfEventAction ( const G4Event * )
overridevirtual

Definition at line 47 of file MaterialStepRecorder.cxx.

48 {
49 ATH_MSG_DEBUG(" BeginOfEventAction");
50
51 // create a new Collection
53 // m_eventStepLength = 0;
54 }
#define ATH_MSG_DEBUG(x)
DataVector< Trk::MaterialStep > MaterialStepCollection

◆ BeginOfRunAction()

void MaterialStepRecorder::BeginOfRunAction ( const G4Run * )
overridevirtual

Definition at line 75 of file MaterialStepRecorder.cxx.

76 {
77 ATH_MSG_DEBUG(" BeginOfRunAction");
78
79 // initialize
80 m_totalSteps = 0;
81 m_eventID = 0;
82 }

◆ EndOfEventAction()

void MaterialStepRecorder::EndOfEventAction ( const G4Event * )
overridevirtual

Definition at line 56 of file MaterialStepRecorder.cxx.

57 {
58 ATH_MSG_DEBUG(" EndOfEventAction");
59
60 ++m_eventID;
61 // write the Collection to StoreGate
62 if(m_evtStore->record(m_matStepCollection, m_matStepCollectionName, false).isFailure())
63 {
64 ATH_MSG_ERROR("cannot record step collection to StoreGate");
65 }
66 // write out the ElementTable of this event
67 if ( m_evtStore->record(m_elementTable, m_elementTableName, false).isFailure() )
68 {
69 ATH_MSG_ERROR("EndOfEventAction : recording ElementTable in StoreGate FAILED");
70 delete m_elementTable;
71 }
72 m_elementTable = nullptr;
73 }
#define ATH_MSG_ERROR(x)

◆ initMessaging()

void AthMessaging::initMessaging ( ) const
privateinherited

Initialize our message level and MessageSvc.

This method should only be called once.

Definition at line 39 of file AthMessaging.cxx.

40{
42 // If user did not set an explicit level, set a default
43 if (m_lvl == MSG::NIL) {
44 m_lvl = m_imsg ?
45 static_cast<MSG::Level>( m_imsg.load()->outputLevel(m_nm) ) :
46 MSG::INFO;
47 }
48}
std::string m_nm
Message source name.
std::atomic< IMessageSvc * > m_imsg
MessageSvc pointer.
std::atomic< MSG::Level > m_lvl
Current logging level.
IMessageSvc * getMessageSvc(bool quiet=false)

◆ msg() [1/2]

MsgStream & AthMessaging::msg ( ) const
inlineinherited

The standard message stream.

Returns a reference to the default message stream May not be invoked before sysInitialize() has been invoked.

Definition at line 163 of file AthMessaging.h.

164{
165 MsgStream* ms = m_msg_tls.get();
166 if (!ms) {
167 if (!m_initialized.test_and_set()) initMessaging();
168 ms = new MsgStream(m_imsg,m_nm);
169 m_msg_tls.reset( ms );
170 }
171
172 ms->setLevel (m_lvl);
173 return *ms;
174}
boost::thread_specific_ptr< MsgStream > m_msg_tls
MsgStream instance (a std::cout like with print-out levels)
void initMessaging() const
Initialize our message level and MessageSvc.

◆ msg() [2/2]

MsgStream & AthMessaging::msg ( const MSG::Level lvl) const
inlineinherited

The standard message stream.

Returns a reference to the default message stream May not be invoked before sysInitialize() has been invoked.

Definition at line 178 of file AthMessaging.h.

179{ return msg() << lvl; }
MsgStream & msg() const
The standard message stream.

◆ msgLvl()

bool AthMessaging::msgLvl ( const MSG::Level lvl) const
inlineinherited

Test the output level.

Parameters
lvlThe message level to test against
Returns
boolean Indicating if messages at given level will be printed
Return values
trueMessages at level "lvl" will be printed

Definition at line 151 of file AthMessaging.h.

152{
153 if (m_lvl <= lvl) {
154 msg() << lvl;
155 return true;
156 } else {
157 return false;
158 }
159}

◆ setLevel()

void AthMessaging::setLevel ( MSG::Level lvl)
inherited

Change the current logging level.

Use this rather than msg().setLevel() for proper operation with MT.

Definition at line 28 of file AthMessaging.cxx.

29{
30 m_lvl = lvl;
31}

◆ UserSteppingAction()

void MaterialStepRecorder::UserSteppingAction ( const G4Step * aStep)
overridevirtual

Definition at line 84 of file MaterialStepRecorder.cxx.

85 {
86 // kill secondaries
87 if (aStep->GetTrack()->GetParentID()) {
88 aStep->GetTrack()->SetTrackStatus(fStopAndKill);
89 return;
90 }
91
92 // ElementTable preparation
94 m_elementTable = new Trk::ElementTable();
95 m_runElementTable = new Trk::ElementTable();
96 }
97
98 // the material information
99 const G4TouchableHistory* touchHist = static_cast<const G4TouchableHistory*>(aStep->GetPreStepPoint()->GetTouchable());
100 // G4LogicalVolume
101 const G4LogicalVolume *lv= touchHist ? touchHist->GetVolume()->GetLogicalVolume() : nullptr;
102 const G4Material *mat = lv ? lv->GetMaterial() : nullptr;
103
104 std::vector<unsigned char> elements;
105 std::vector<unsigned char> fractions;
106
107 // log the information // cut off air
108 if (mat && mat->GetRadlen() < 200000.) {
109
110 ++m_totalSteps;
111
112 // the step information
113 double steplength = aStep->GetStepLength();
114
115 // the position information
116 G4ThreeVector pos = aStep->GetPreStepPoint()->GetPosition();
117
118 double X0 = mat->GetRadlen();
119 double L0 = mat->GetNuclearInterLength();
120 double A = 0.;
121 double Z = 0.;
122 // double totAtoms = 0.;
123
124 double rho = mat->GetDensity()*CLHEP::mm3/CLHEP::gram;
125
126 // get the number of Elements
127 size_t elNumber = mat->GetNumberOfElements();
128 const G4ElementVector* elVector = mat->GetElementVector();
129 double totAtoms = mat->GetTotNbOfAtomsPerVolume();
130
131 // reserve the right number of elements
132 elements.reserve(elNumber);
133 fractions.reserve(elNumber);
134
135 if (1 == elNumber) {
136
137 A = mat->GetA()/CLHEP::gram;
138 Z = mat->GetZ();
139
140 unsigned int Zint = (unsigned int)Z;
141 // the element and fraction vector
142 elements.push_back(static_cast<unsigned char>(Z));
143 fractions.push_back(UCHAR_MAX);
144 // record the Element
145 if (m_recordComposition && !m_elementTable->contains(Zint)){
146 // the element Material
147 Trk::Material elMat(X0,L0,A,Z,rho);
148 G4String elName = (*elVector)[0]->GetName();
149 // add it to the table
150 m_elementTable->addElement(elMat, elName);
151 m_runElementTable->addElement(elMat,elName);
152 }
153
154
155 } else {
156
157 const G4double* atVector = mat->GetVecNbOfAtomsPerVolume();
158 double totalFrac = 0.;
159
160 for (size_t iel = 0; iel < elNumber; ++iel) {
161
162 const G4Element* currentEl = (*elVector)[iel];
163 double currentNum = atVector ? atVector[iel] : 1.;
164 double relNbAtoms = currentNum/totAtoms;
165
166 double currentZ = currentEl->GetZ();
167
168 A += relNbAtoms*currentEl->GetA()/CLHEP::gram;
169 Z += relNbAtoms*currentEl->GetZ();
170 unsigned int Zint = (unsigned int)(currentEl->GetZ());
171
172 // the element and fraction vector
173 elements.push_back(int(currentZ));
174 // calculate the fraction with a accuracy of 1/256.
175 unsigned int relNbAtomsChar = (unsigned int)(relNbAtoms*(UCHAR_MAX));
176 relNbAtomsChar = relNbAtomsChar > UCHAR_MAX ? UCHAR_MAX : relNbAtomsChar;
177
178 // smaller components than 0.5 % are automatically ignored
179 totalFrac += relNbAtoms;
180 if (relNbAtomsChar) {
181 fractions.push_back(relNbAtomsChar);
182 // record composition
183 if (m_recordComposition && !m_elementTable->contains(Zint)){
184 double curA = currentEl->GetA()/CLHEP::gram;
185 double curZ = currentEl->GetZ();
186 // approximate formulas for X0 and L0
187 // X0 from : PH-EP-Tech-Note-2010-013 g/cm3 -> g/mm3
188 // L0 from :
189 double curX0 = 1432.8*curA/(curZ*(curZ+1)*(11.319-std::log(curZ)));
190 double curL0 = 0.;
191 double curRho = rho*relNbAtoms;
192 Trk::Material elMat(curX0,curL0,curA,curZ,curRho);
193 const G4String& elName = currentEl->GetName();
194 // add it to the table
195 m_elementTable->addElement(elMat, elName);
196 m_runElementTable->addElement(elMat,elName);
197 }
198 }
199 }
200 if ((totalFrac-1.)*(totalFrac-1.) > 10e-4 )
201 ATH_MSG_DEBUG("Total fractions do not add up to one at INPUT (" << totalFrac << ") !");
202 }
203
204 // is it a Geantino?
205 if (aStep->GetTrack()->GetParticleDefinition()->GetPDGEncoding() == 0) {
207 m_matStepCollection->push_back(new Trk::MaterialStep(pos.x(), pos.y(), pos.z(), steplength, X0, L0, A, Z, rho, elements, fractions));
208 else
209 m_matStepCollection->push_back(new Trk::MaterialStep(pos.x(), pos.y(), pos.z(), steplength, X0, L0, A, Z, rho));
210 }
211 }
212 }

Member Data Documentation

◆ ATLAS_THREAD_SAFE

std::atomic_flag m_initialized AthMessaging::ATLAS_THREAD_SAFE = ATOMIC_FLAG_INIT
mutableprivateinherited

Messaging initialized (initMessaging)

Definition at line 141 of file AthMessaging.h.

◆ m_elementTable

Trk::ElementTable* G4UA::MaterialStepRecorder::m_elementTable
private

Definition at line 61 of file MaterialStepRecorder.h.

◆ m_elementTableName

std::string G4UA::MaterialStepRecorder::m_elementTableName
private

Definition at line 62 of file MaterialStepRecorder.h.

◆ m_eventID

size_t G4UA::MaterialStepRecorder::m_eventID
private

Definition at line 59 of file MaterialStepRecorder.h.

◆ m_evtStore

ServiceHandle<StoreGateSvc> G4UA::MaterialStepRecorder::m_evtStore
private

Pointer to StoreGate (event store by default)

Definition at line 51 of file MaterialStepRecorder.h.

◆ m_imsg

std::atomic<IMessageSvc*> AthMessaging::m_imsg { nullptr }
mutableprivateinherited

MessageSvc pointer.

Definition at line 135 of file AthMessaging.h.

135{ nullptr };

◆ m_lvl

std::atomic<MSG::Level> AthMessaging::m_lvl { MSG::NIL }
mutableprivateinherited

Current logging level.

Definition at line 138 of file AthMessaging.h.

138{ MSG::NIL };

◆ m_matStepCollection

Trk::MaterialStepCollection* G4UA::MaterialStepRecorder::m_matStepCollection
private

Definition at line 53 of file MaterialStepRecorder.h.

◆ m_matStepCollectionName

std::string G4UA::MaterialStepRecorder::m_matStepCollectionName
private

Definition at line 54 of file MaterialStepRecorder.h.

◆ m_msg_tls

boost::thread_specific_ptr<MsgStream> AthMessaging::m_msg_tls
mutableprivateinherited

MsgStream instance (a std::cout like with print-out levels)

Definition at line 132 of file AthMessaging.h.

◆ m_nm

std::string AthMessaging::m_nm
privateinherited

Message source name.

Definition at line 129 of file AthMessaging.h.

◆ m_recordComposition

bool G4UA::MaterialStepRecorder::m_recordComposition
private

Definition at line 56 of file MaterialStepRecorder.h.

◆ m_runElementTable

Trk::ElementTable* G4UA::MaterialStepRecorder::m_runElementTable
private

Definition at line 64 of file MaterialStepRecorder.h.

◆ m_totalSteps

size_t G4UA::MaterialStepRecorder::m_totalSteps
private

Definition at line 58 of file MaterialStepRecorder.h.


The documentation for this class was generated from the following files: