Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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
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;
63
64using G4ITReactionList = std::list<G4ITReactionPtr>;
69 G4ITReactionList::iterator> >;
70
76
77using G4ITReactionPerTime = std::multiset<G4ITReactionPtr, compReactionPerTime>;
78using G4ITReactionPerTimeIt = std::multiset<G4ITReactionPtr, compReactionPerTime>::iterator;
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.emplace_back(reactionPerTrack, it);
109 }
110
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:
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
231 reactionPerTrack = it_track->second;
232 auto list = reactionPerTrack->GetReactionList();
233 //for(auto it_list = list.begin(); it_list != list.end(); ++it_list)
234 for(const auto& it_list:list)
235 {
236 if ((*it_list).GetReactant(trackA)->GetTrackID() == trackB->GetTrackID())
237 {
238 return false;
239 }
240 }
241 return true;
242 }
243
244 void AddReactions(G4double time, G4Track* trackA, G4TrackVectorHandle reactants)
245 {
246 auto it = reactants->begin();
247 for(;it != reactants->end() ; ++it)
248 {
249 AddReaction(time, trackA, *it);
250 }
251 }
252
254 {
255 auto it = fReactionPerTrack.find(track);
256 if(it != fReactionPerTrack.end())
257 {
258 G4ITReactionPerTrackPtr backItUp = it->second->shared_from_this();
259 backItUp->RemoveMe();
260 //fReactionPerTrack.erase(it); // not needed : once empty ==> auto-erase
261 it = fReactionPerTrack.find(track);
262 if(it != fReactionPerTrack.end())
263 {
264 fReactionPerTrack.erase(it);
265 }
266 }
267 }
268
270 {
271 reaction->RemoveMe();
272 RemoveReactionSet(reaction->GetReactants().first);
273 RemoveReactionSet(reaction->GetReactants().second);
274 }
275
280
282 {
283 for(auto & it : reactionPerTrack->GetListOfIterators())
284 {
285 fReactionPerTrack.erase(it);
286 }
287 reactionPerTrack->GetListOfIterators().clear();
288 reactionPerTrack->GetReactionList().clear();
289 }
290
292 {
293 for(auto it = fReactionPerTrack.begin();
294 it != fReactionPerTrack.end() ;
295 it = fReactionPerTrack.begin())
296 {
297 it->second->RemoveMe();
298 }
299 fReactionPerTrack.clear();
300 fReactionPerTime.clear();
301 }
302
304 {
305 return fReactionPerTrack.empty();
306 }
307
312
314 fSortByTime = true;
315 }
316
317protected:
318 void AddReaction(G4Track* track, G4ITReactionPtr reaction)
319 {
320 auto it = fReactionPerTrack.find(track);
321
322 G4ITReactionPerTrackPtr reactionPerTrack;
323
324 if(it == fReactionPerTrack.end())
325 {
326 reactionPerTrack = G4ITReactionPerTrack::New();
327 std::pair< G4ITReactionPerTrackMap::iterator,bool> pos =
328 fReactionPerTrack.insert(std::make_pair(track, reactionPerTrack));
329 reactionPerTrack->AddIterator(pos.first);
330 }
331 else
332 {
333 reactionPerTrack = it->second;
334 }
335
336 reactionPerTrack->AddReaction(reaction);
337 }
340
343};
344
345#endif /* G4ITREACTIONINFO_HH_ */
std::list< std::pair< G4ITReactionPerTrackPtr, G4ITReactionList::iterator > > G4ReactionPerTrackIt
std::map< G4Track *, G4ITReactionPerTrackPtr, compTrackPerID > G4ITReactionPerTrackMap
std::multiset< G4ITReactionPtr, compReactionPerTime > G4ITReactionPerTime
G4shared_ptr< G4ITReaction > G4ITReactionPtr
std::list< G4ITReactionPtr > G4ITReactionList
G4shared_ptr< std::vector< G4Track * > > G4TrackVectorHandle
G4shared_ptr< G4ITReactionPerTrack > G4ITReactionPerTrackPtr
std::multiset< G4ITReactionPtr, compReactionPerTime >::iterator G4ITReactionPerTimeIt
double G4double
Definition G4Types.hh:83
bool G4bool
Definition G4Types.hh:86
#define G4shared_ptr
Definition G4memory.hh:32
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 AddReactions(G4double time, G4Track *trackA, G4TrackVectorHandle reactants)
G4ITReactionPerTime fReactionPerTime
G4ITReactionPerTrackMap & GetReactionMap()
G4ITReactionPerTrackMap fReactionPerTrack
G4ITReactionPerTime & GetReactionsPerTime()
std::pair< G4Track *, G4Track * > GetReactants() const
virtual ~G4ITReaction()
std::pair< G4Track *, G4Track * > fReactants
G4ReactionPerTrackIt fReactionPerTrack
G4ITReactionPerTimeIt * fReactionPerTimeIt
void AddIterator(G4ITReactionPerTrackPtr reactionPerTrack, G4ITReactionList::iterator it)
std::size_t GetHash() const
static G4ITReactionPtr New(G4double time, G4Track *trackA, G4Track *trackB)
G4double GetTime() const
void AddIterator(G4ITReactionPerTimeIt it)
G4Track * GetReactant(G4Track *trackA) const
G4int GetTrackID() const
G4bool operator()(G4ITReactionPtr rhs, G4ITReactionPtr lhs) const
G4bool operator()(G4Track *rhs, G4Track *lhs) const
#define G4ThreadLocal
Definition tls.hh:77