Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4ITReaction.hh
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26/*
27 * G4ITReactionInfo.hh
28 *
29 * Created on: 1 févr. 2015
30 * Author: matkara
31 * updated: HoangTran
32 */
33
34#ifndef G4ITREACTIONINFO_HH_
35#define G4ITREACTIONINFO_HH_
36
37#include "tls.hh"
38#include <list>
39#include <map>
40#include <G4memory.hh>
41#include "G4Track.hh"
42#include <set>
43
44typedef G4shared_ptr< std::vector<G4Track*> > G4TrackVectorHandle;
45
46#ifndef compTrackPerID__
47#define compTrackPerID__
49{
50 G4bool operator()(G4Track* rhs, G4Track* lhs) const
51 {
52 return rhs->GetTrackID() < lhs->GetTrackID();
53 }
54};
55#endif
56
57class G4Track;
58class G4ITReactionSet;
60class G4ITReaction;
61typedef G4shared_ptr<G4ITReaction> G4ITReactionPtr;
62typedef G4shared_ptr<G4ITReactionPerTrack> G4ITReactionPerTrackPtr;
63
64typedef std::list<G4ITReactionPtr> G4ITReactionList;
65typedef std::map<G4Track*,
68typedef std::list<std::pair<G4ITReactionPerTrackPtr,
69 G4ITReactionList::iterator> > G4ReactionPerTrackIt;
70
72{
74 G4ITReactionPtr lhs) const;
75};
76
77typedef std::multiset<G4ITReactionPtr, compReactionPerTime> G4ITReactionPerTime;
78typedef std::multiset<G4ITReactionPtr, compReactionPerTime>::iterator G4ITReactionPerTimeIt;
79
80class G4ITReaction : public G4enable_shared_from_this<G4ITReaction>
81{
83public:
84 static G4ITReactionPtr New(G4double time, G4Track* trackA, G4Track* trackB)
85 {
86 return G4ITReactionPtr(new G4ITReaction(time, trackA, trackB));
87 }
88 virtual ~G4ITReaction();
89
90 G4Track* GetReactant(G4Track* trackA) const
91 {
92 if(fReactants.first != trackA) return fReactants.first;
93 return fReactants.second;
94 }
95
96 std::pair<G4Track*, G4Track*> GetReactants() const{return fReactants;}
97 std::size_t GetHash() const;
99 {
100 return fTime;
101 }
102
103 void RemoveMe();
104
106 G4ITReactionList::iterator it)
107 {
108 fReactionPerTrack.push_back(std::make_pair(reactionPerTrack, it));
109 }
110
112 {
114 }
115
117 std::pair<G4Track*, G4Track*> fReactants;
120
121 //static G4ThreadLocal std::set<G4ITReaction*>* gAll;
122};
123
124class G4ITReactionPerTrack : public G4enable_shared_from_this<G4ITReactionPerTrack>
125{
126 G4ITReactionPerTrack() = default;
127public:
129 {
131 }
132
134 {
135 fReactions.clear();
136 }
137
139 {
140 auto it =
141 fReactions.insert(fReactions.end(), reaction);
142 reaction->AddIterator(this->shared_from_this(), it);
143 }
144
145 void AddIterator(G4ITReactionPerTrackMap::iterator it)
146 {
147 fReactionSetIt.push_back(it);
148 }
149
150 G4bool RemoveThisReaction(G4ITReactionList::iterator it);
151 void RemoveMe()
152 {
153 G4ITReactionPerTrackPtr backMeUp = this->shared_from_this();
154
155 G4ITReactionList::iterator next;
156 for(auto it = fReactions.begin() ;
157 it != fReactions.end() ; it = next)
158 {
159 next = it;
160 ++next;
161 (*it)->RemoveMe();
162 }
163 fReactions.clear();
164 fReactionSetIt.clear();
165 }
166
168 {
169 return fReactions;
170 }
171
172 std::list<G4ITReactionPerTrackMap::iterator>& GetListOfIterators()
173 {
174 return fReactionSetIt;
175 }
176
177protected:
179 std::list<G4ITReactionPerTrackMap::iterator> fReactionSetIt;
180};
181
183{
185 {
186 fpInstance = this;
187 fSortByTime = false;
188 }
189public:
191 {
192 fReactionPerTrack.clear();
193 fReactionPerTime.clear();
194 }
195
197 {
198 if(fpInstance == nullptr) new G4ITReactionSet();
199
200 return fpInstance;
201 }
202
203 //------------------------------------------------------------------------------------
204
205 void AddReaction(G4double time, G4Track* trackA, G4Track* trackB)
206 {
207 if(CanAddThisReaction(trackA, trackB))
208 {
209 G4ITReactionPtr reaction(G4ITReaction::New(time, trackA, trackB));
210 AddReaction(trackA, reaction);
211 AddReaction(trackB, reaction);
212
213 if(fSortByTime)
214 {
215 auto it = fReactionPerTime.insert(reaction);
216 reaction->AddIterator(it);
217 }
218 }
219 }
220
221//Hoang: this function checks if this reaction is added
223 {
224 auto it_track = fReactionPerTrack.find(trackA);
225 G4ITReactionPerTrackPtr reactionPerTrack;
226 if(it_track == fReactionPerTrack.end())
227 {
228 return true;
229 }
230 else
231 {
232 reactionPerTrack = it_track->second;
233 auto list = reactionPerTrack->GetReactionList();
234 //for(auto it_list = list.begin(); it_list != list.end(); ++it_list)
235 for(const auto& it_list:list)
236 {
237 if ((*it_list).GetReactant(trackA)->GetTrackID() == trackB->GetTrackID())
238 {
239 return false;
240 }
241 }
242 return true;
243 }
244 }
245
246 void AddReactions(G4double time, G4Track* trackA, G4TrackVectorHandle reactants)
247 {
248 auto it = reactants->begin();
249 for(;it != reactants->end() ; ++it)
250 {
251 AddReaction(time, trackA, *it);
252 }
253 }
254
256 {
257 auto it = fReactionPerTrack.find(track);
258 if(it != fReactionPerTrack.end())
259 {
260 G4ITReactionPerTrackPtr backItUp = it->second->shared_from_this();
261 backItUp->RemoveMe();
262 //fReactionPerTrack.erase(it); // not needed : once empty ==> auto-erase
263 it = fReactionPerTrack.find(track);
264 if(it != fReactionPerTrack.end())
265 {
266 fReactionPerTrack.erase(it);
267 }
268 }
269 }
270
272 {
273 reaction->RemoveMe();
274 RemoveReactionSet(reaction->GetReactants().first);
275 RemoveReactionSet(reaction->GetReactants().second);
276 }
277
279 {
280 return fReactionPerTrack;
281 }
282
284 {
285 for(auto it =
286 reactionPerTrack->GetListOfIterators().begin() ;
287 it != reactionPerTrack->GetListOfIterators().end() ;
288 ++it)
289 {
290 fReactionPerTrack.erase(*it);
291 }
292 reactionPerTrack->GetListOfIterators().clear();
293 reactionPerTrack->GetReactionList().clear();
294 }
295
297 {
298 for(auto it = fReactionPerTrack.begin();
299 it != fReactionPerTrack.end() ;
300 it = fReactionPerTrack.begin())
301 {
302 it->second->RemoveMe();
303 }
304 fReactionPerTrack.clear();
305 fReactionPerTime.clear();
306 }
307
309 {
310 return fReactionPerTrack.empty();
311 }
312
314 {
315 return fReactionPerTime;
316 }
317
319 fSortByTime = true;
320 }
321
322protected:
323 void AddReaction(G4Track* track, G4ITReactionPtr reaction)
324 {
325 auto it = fReactionPerTrack.find(track);
326
327 G4ITReactionPerTrackPtr reactionPerTrack;
328
329 if(it == fReactionPerTrack.end())
330 {
331 reactionPerTrack = G4ITReactionPerTrack::New();
332 std::pair< G4ITReactionPerTrackMap::iterator,bool> pos =
333 fReactionPerTrack.insert(std::make_pair(track, reactionPerTrack));
334 reactionPerTrack->AddIterator(pos.first);
335 }
336 else
337 {
338 reactionPerTrack = it->second;
339 }
340
341 reactionPerTrack->AddReaction(reaction);
342 }
345
348};
349
350#endif /* G4ITREACTIONINFO_HH_ */
G4shared_ptr< G4ITReactionPerTrack > G4ITReactionPerTrackPtr
Definition: G4ITReaction.hh:62
std::list< G4ITReactionPtr > G4ITReactionList
Definition: G4ITReaction.hh:64
G4shared_ptr< G4ITReaction > G4ITReactionPtr
Definition: G4ITReaction.hh:61
G4shared_ptr< std::vector< G4Track * > > G4TrackVectorHandle
Definition: G4ITReaction.hh:44
std::multiset< G4ITReactionPtr, compReactionPerTime > G4ITReactionPerTime
Definition: G4ITReaction.hh:77
std::map< G4Track *, G4ITReactionPerTrackPtr, compTrackPerID > G4ITReactionPerTrackMap
Definition: G4ITReaction.hh:67
std::list< std::pair< G4ITReactionPerTrackPtr, G4ITReactionList::iterator > > G4ReactionPerTrackIt
Definition: G4ITReaction.hh:69
std::multiset< G4ITReactionPtr, compReactionPerTime >::iterator G4ITReactionPerTimeIt
Definition: G4ITReaction.hh:78
double G4double
Definition: G4Types.hh:83
bool G4bool
Definition: G4Types.hh:86
#define G4enable_shared_from_this
Definition: G4memory.hh:37
void AddIterator(G4ITReactionPerTrackMap::iterator it)
G4ITReactionList fReactions
G4bool RemoveThisReaction(G4ITReactionList::iterator it)
std::list< G4ITReactionPerTrackMap::iterator > & GetListOfIterators()
void AddReaction(G4ITReactionPtr reaction)
G4ITReactionList & GetReactionList()
std::list< G4ITReactionPerTrackMap::iterator > fReactionSetIt
virtual ~G4ITReactionPerTrack()
static G4ITReactionPerTrackPtr New()
void AddReaction(G4double time, G4Track *trackA, G4Track *trackB)
void AddReaction(G4Track *track, G4ITReactionPtr reaction)
static G4ThreadLocal G4ITReactionSet * fpInstance
void RemoveReactionSet(G4Track *track)
virtual ~G4ITReactionSet()
G4bool CanAddThisReaction(G4Track *trackA, G4Track *trackB)
void RemoveReactionPerTrack(G4ITReactionPerTrackPtr reactionPerTrack)
static G4ITReactionSet * Instance()
void SelectThisReaction(G4ITReactionPtr reaction)
void CleanAllReaction()
void AddReactions(G4double time, G4Track *trackA, G4TrackVectorHandle reactants)
G4ITReactionPerTime fReactionPerTime
G4ITReactionPerTrackMap & GetReactionMap()
G4ITReactionPerTrackMap fReactionPerTrack
G4ITReactionPerTime & GetReactionsPerTime()
std::pair< G4Track *, G4Track * > GetReactants() const
Definition: G4ITReaction.hh:96
virtual ~G4ITReaction()
Definition: G4ITReaction.cc:79
std::pair< G4Track *, G4Track * > fReactants
void RemoveMe()
Definition: G4ITReaction.cc:85
G4ReactionPerTrackIt fReactionPerTrack
G4ITReactionPerTimeIt * fReactionPerTimeIt
void AddIterator(G4ITReactionPerTrackPtr reactionPerTrack, G4ITReactionList::iterator it)
std::size_t GetHash() const
Definition: G4ITReaction.cc:61
static G4ITReactionPtr New(G4double time, G4Track *trackA, G4Track *trackB)
Definition: G4ITReaction.hh:84
G4double GetTime() const
Definition: G4ITReaction.hh:98
void AddIterator(G4ITReactionPerTimeIt it)
G4Track * GetReactant(G4Track *trackA) const
Definition: G4ITReaction.hh:90
G4double fTime
G4int GetTrackID() const
G4bool operator()(G4ITReactionPtr rhs, G4ITReactionPtr lhs) const
Definition: G4ITReaction.cc:40
G4bool operator()(G4Track *rhs, G4Track *lhs) const
Definition: G4ITReaction.hh:50
#define G4ThreadLocal
Definition: tls.hh:77