Geant4  10.02.p02
G4ITFinder.icc
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 // $Id$
27 //
28 // Author: Mathieu Karamitros (kara (AT) cenbg . in2p3 . fr)
29 //
30 // WARNING : This class is released as a prototype.
31 // It might strongly evolve or even disapear in the next releases.
32 //
33 // History:
34 // -----------
35 // 10 Oct 2011 M.Karamitros created
36 //
37 // -------------------------------------------------------------------
38 
39 TEMPLATE
40 G4ThreadLocal G4ITMANAGER * G4ITMANAGER::fInstance = 0;
41 
42 TEMPLATE
43 G4ITMANAGER * G4ITMANAGER::Instance()
44 {
45  if (!fInstance) fInstance = new G4ITFinder();
46 
47  return fInstance;
48 }
49 
50 TEMPLATE G4ITMANAGER::G4ITFinder() : G4VITFinder()
51 {
52  fVerbose = 0;
53  G4AllITFinder::Instance()->RegisterManager(this);
54 }
55 
56 TEMPLATE G4ITMANAGER::~G4ITFinder()
57 {
58  Clear();
59  fInstance = 0;
60 }
61 
62 TEMPLATE
63 void G4ITMANAGER::Clear()
64 {
65  {
66 
67  TreeMap::iterator it;
68 
69  for (it = fTree.begin(); it != fTree.end(); it++)
70  {
71  if (it->second) delete it->second;
72  }
73 
74  fTree.clear();
75  }
76 }
77 
78 TEMPLATE
79 void G4ITMANAGER::iUpdatePositionMap()
80 {
81  fInstance->UpdatePositionMap();
82 }
83 
84 TEMPLATE
85 void G4ITMANAGER::Push(G4Track* track)
86 {
87  /*
88  * If you want to use this class with another type than G4Molecule
89  * inheriting as well from G4IT, replace T::GetMolecule by GetIT
90  * and aIT->GetMoleculeID by GetSubITType
91  */
92  T* aIT = T::GetMolecule(track);
93  aIT->RecordCurrentPositionNTime();
94 
95  int key = aIT->GetMoleculeID();
96 
97  if (!(aIT->GetNode()))
98  {
99  G4KDNode_Base* node = 0;
100 
101  TreeMap::iterator it_fTree = fTree.find(key);
102 
103  if (it_fTree != fTree.end())
104  {
105  node = it_fTree->second->Insert(aIT);
106  }
107  else
108  {
109  G4KDTree* aTree = new G4KDTree();
110  fTree.insert(std::make_pair(key, aTree));
111  node = aTree->Insert(aIT);
112  }
113 
114  aIT->SetNode(node);
115  }
116 }
117 
118 TEMPLATE
119 G4KDTreeResultHandle G4ITMANAGER::FindNearest(const G4ThreeVector& position,
120  int key)
121 {
122  TreeMap::iterator it = fTree.find(key);
123  if (it != fTree.end()) return it->second->Nearest(position);
124  else
125  {
126  return 0;
127  }
128  return 0;
129 }
130 
131 TEMPLATE
132 G4KDTreeResultHandle G4ITMANAGER::FindNearest(const T* point0,
133  int key)
134 {
135  if (int(*point0) == key)
136  {
137  // DEBUG
138  // G4cout << "Equal keys !"<< G4endl;
139  G4KDNode_Base* node0 = point0->GetNode();
140 
141  if (node0 == 0)
142  {
143  G4ExceptionDescription exceptionDescription(
144  "Bad request : no node found in the IT you are searching "
145  "closest neighbourg for");
146  G4Exception("G4ITManager::FindNearest", "ITManager002",
147  FatalErrorInArgument, exceptionDescription);
148  return 0; // coverity
149  }
150 
151  TreeMap::iterator it = fTree.find(key);
152  if (it != fTree.end())
153  {
154  G4KDTreeResultHandle output(it->second->Nearest(node0));
155 
156  if (!output)
157  {
158  /*
159  * G4cout << "NO OUTPUT "
160  * << point0->GetName()
161  * << " " << key -> GetName()
162  * << G4endl;
163  */
164  return 0;
165  }
166  // G4cout << "OUTPUT" << G4endl;
167  return output;
168  }
169  else
170  {
171  // DEBUG
172  // G4cout << "Pas trouve dans la map"<< key->GetName() << G4endl;
173  return 0;
174  }
175  }
176  else
177  {
178  // DEBUG
179  // G4cout << "Not equal keys !"<< G4endl;
180  // const G4ThreeVector& position = point0->GetTrack()->GetPosition() ;
181  TreeMap::iterator it = fTree.find(key);
182 
183  if (it != fTree.end())
184  {
185  G4KDTreeResultHandle output(it->second->Nearest(*point0));
186  if (!output)
187  {
188  // G4cout << "NO OUTPUT" << G4endl;
189  return 0;
190  }
191 
192  // G4cout << "OUTPUT" << G4endl;
193  return output;
194  }
195  else
196  {
197  // DEBUG
198  // G4cout << "Pas trouve dans la map : "<< key->GetName() << G4endl;
199  return 0;
200  }
201  }
202  return 0;
203 }
204 
205 TEMPLATE
206 G4KDTreeResultHandle
207 G4ITMANAGER::FindNearest(const T* source,
208  const T* type)
209 {
210  return FindNearest(source,
211  int(*type));
212 }
213 
214 
215 TEMPLATE
216 G4KDTreeResultHandle
217 G4ITMANAGER::FindNearestInRange(const G4ThreeVector& position,
218  int key,
219  G4double R)
220 {
221  TreeMap::iterator it = fTree.find(key);
222  if (it != fTree.end()) return it->second->NearestInRange(position, R);
223  else
224  {
225  return 0;
226  }
227  return 0;
228 }
229 
230 TEMPLATE
231 G4KDTreeResultHandle G4ITMANAGER::FindNearestInRange(const T* point0,
232  int key,
233  G4double R)
234 {
235  if (point0->GetMoleculeID() == key)
236  {
237  G4KDNode_Base* node0 = point0->GetNode();
238  TreeMap::iterator it = fTree.find(key);
239  if (it != fTree.end()) return it->second->NearestInRange(node0, R);
240  else
241  {
242  return 0;
243  }
244  }
245  else
246  {
247  TreeMap::iterator it = fTree.find(key);
248  if (it != fTree.end()) return it->second->NearestInRange(*point0, R);
249  else
250  {
251  return 0;
252  }
253  }
254  return 0;
255 }
256 
257 //#define DEBUG_MEM
258 
259 #ifdef DEBUG_MEM
260 #include "G4MemStat.hh"
261 using namespace G4MemStat;
262 #endif
263 
264 TEMPLATE
265 void G4ITMANAGER::UpdatePositionMap()
266 {
267  G4KDTree* currentTree = 0;
268  G4IT* currentIT = 0;
269  G4KDNode_Base* currentNode = 0;
270 
271 #if defined (DEBUG_MEM)
272  MemStat mem_first, mem_second, mem_diff;
273 #endif
274 
275 #if defined (DEBUG_MEM)
276  mem_first = MemoryUsage();
277 #endif
278 
279  std::map<int,PriorityList*>& listMap = G4ITTrackHolder::Instance()->GetLists();
280  std::map<int,PriorityList*>::iterator it = listMap.begin();
281  std::map<int,PriorityList*>::iterator end = listMap.end();
282 
283  TreeMap::iterator it_Tree;
284 
285  for (; it!= end; it++)
286  {
287  currentTree = 0;
288  int key = it->first;
289  it_Tree = fTree.find(key);
290 
291  // G4cout << "Box : " << it_Box->first.GetName() << G4endl;
292  // G4cout << "N tracks : " << currentBox->GetNTrack() << G4endl;
293 
294  // G4cout << "Will update : " << it_Box->first.GetName() <<G4endl;
295  if (it_Tree != fTree.end())
296  {
297  currentTree = it_Tree->second;
298  if (currentTree)
299  {
300  currentTree->Clear();
301  //delete currentTree;
302  //currentTree = 0;
303 
304 #if defined (DEBUG_MEM)
305  mem_second = MemoryUsage();
306  mem_diff = mem_second-mem_first;
307  G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
308  "after currentTree->Clear(), diff is : " << mem_diff << G4endl;
309 #endif
310  }
311  }
312 
313  PriorityList* listUnion=it->second;
314 
315  if(listUnion == 0 || listUnion->GetMainList() == 0
316  || listUnion->GetMainList()->empty() )
317  {
318 #if defined (DEBUG_MEM)
319  mem_second = MemoryUsage();
320  mem_diff = mem_second-mem_first;
321  G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
322  "In if(currentBox->Empty()), diff is : " << mem_diff << G4endl;
323 #endif
324 
325  continue;
326  }
327  else
328  {
329  if (currentTree == 0)
330  {
331  currentTree = new G4KDTree();
332  fTree[key] = currentTree;
333 
334 // G4cout << "**** " << "Create new tree for : " << key.GetName()
335 // << G4endl;
336 
337 #if defined (DEBUG_MEM)
338  mem_second = MemoryUsage();
339  mem_diff = mem_second-mem_first;
340  G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
341  "after creating tree, diff is : " << mem_diff << G4endl;
342 #endif
343  }
344 
345  G4TrackList* trackList = listUnion->GetMainList();
346  G4TrackList::iterator __it = trackList->begin();
347  G4TrackList::iterator __end = trackList->end();
348  // int i = 0;
349 
350  // G4cout << "Box : " << key.GetName() << G4endl;
351  // G4cout << "N tracks : " << currentBox->GetNTrack() << G4endl;
352 
353  for (; __it != __end; __it++)//, i++)
354  {
355  // G4cout << i << G4endl;
356  // G4cout << "track "
357  // << currentIT->GetTrack()->GetTrackID()
358  // << G4endl;
359  currentIT = GetIT(*__it);
360  currentNode = currentTree->Insert(currentIT);
361 
362  /*G4KDNode_Base* */
363 // currentNode = currentTree->InsertMap(currentIT);
364 // if(currentIT->GetNode())
365 // {
366 // delete currentIT->GetNode();
367  // No need: deleted when tree is cleared
368 // }
369  currentIT->SetNode(currentNode);
370 
371 #if defined (DEBUG_MEM)
372  mem_second = MemoryUsage();
373  mem_diff = mem_second-mem_first;
374  G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
375  "after currentIT->SetNode(currentNode), diff is : "
376  << mem_diff << G4endl;
377 #endif
378  }
379 
380 #if defined (DEBUG_MEM)
381  mem_second = MemoryUsage();
382  mem_diff = mem_second-mem_first;
383  G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
384  "In else{...}, diff is : " << mem_diff << G4endl;
385 #endif
386 
387  }
388 // currentTree->Build();
389  }
390 }