Geant4  10.01.p03
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  const T* key)
121 {
122  TreeMap::iterator it = fTree.find(key->GetMoleculeID());
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  const T* key)
134 {
135  if (*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->GetMoleculeID());
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->GetMoleculeID());
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::FindNearestInRange(const G4ThreeVector& position,
208  const T* key,
209  G4double R)
210 {
211  TreeMap::iterator it = fTree.find(key->GetMoleculeID());
212  if (it != fTree.end()) return it->second->NearestInRange(position, R);
213  else
214  {
215  return 0;
216  }
217  return 0;
218 }
219 
220 TEMPLATE
221 G4KDTreeResultHandle G4ITMANAGER::FindNearestInRange(const T* point0,
222  const T* key,
223  G4double R)
224 {
225  if (*point0 == *key)
226  {
227  G4KDNode_Base* node0 = point0->GetNode();
228  TreeMap::iterator it = fTree.find(key->GetMoleculeID());
229  if (it != fTree.end()) return it->second->NearestInRange(node0, R);
230  else
231  {
232  return 0;
233  }
234  }
235  else
236  {
237  TreeMap::iterator it = fTree.find(key->GetMoleculeID());
238  if (it != fTree.end()) return it->second->NearestInRange(*point0, R);
239  else
240  {
241  return 0;
242  }
243  }
244  return 0;
245 }
246 
247 //#define DEBUG_MEM
248 
249 #ifdef DEBUG_MEM
250 #include "G4MemStat.hh"
251 using namespace G4MemStat;
252 #endif
253 
254 TEMPLATE
255 void G4ITMANAGER::UpdatePositionMap()
256 {
257  G4KDTree* currentTree = 0;
258  G4IT* currentIT = 0;
259  G4KDNode_Base* currentNode = 0;
260 
261 #if defined (DEBUG_MEM)
262  MemStat mem_first, mem_second, mem_diff;
263 #endif
264 
265 #if defined (DEBUG_MEM)
266  mem_first = MemoryUsage();
267 #endif
268 
269  std::map<int,PriorityList*>& listMap = G4ITTrackHolder::Instance()->GetLists();
270  std::map<int,PriorityList*>::iterator it = listMap.begin();
271  std::map<int,PriorityList*>::iterator end = listMap.end();
272 
273  TreeMap::iterator it_Tree;
274 
275  for (; it!= end; it++)
276  {
277  currentTree = 0;
278  int key = it->first;
279  it_Tree = fTree.find(key);
280 
281  // G4cout << "Box : " << it_Box->first.GetName() << G4endl;
282  // G4cout << "N tracks : " << currentBox->GetNTrack() << G4endl;
283 
284  // G4cout << "Will update : " << it_Box->first.GetName() <<G4endl;
285  if (it_Tree != fTree.end())
286  {
287  currentTree = it_Tree->second;
288  if (currentTree)
289  {
290  currentTree->Clear();
291  //delete currentTree;
292  //currentTree = 0;
293 
294 #if defined (DEBUG_MEM)
295  mem_second = MemoryUsage();
296  mem_diff = mem_second-mem_first;
297  G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
298  "after currentTree->Clear(), diff is : " << mem_diff << G4endl;
299 #endif
300  }
301  }
302 
303  PriorityList* listUnion=it->second;
304 
305  if(listUnion == 0 || listUnion->GetMainList() == 0
306  || listUnion->GetMainList()->empty() )
307  {
308 #if defined (DEBUG_MEM)
309  mem_second = MemoryUsage();
310  mem_diff = mem_second-mem_first;
311  G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
312  "In if(currentBox->Empty()), diff is : " << mem_diff << G4endl;
313 #endif
314 
315  continue;
316  }
317  else
318  {
319  if (currentTree == 0)
320  {
321  currentTree = new G4KDTree();
322  fTree[key] = currentTree;
323 
324 // G4cout << "**** " << "Create new tree for : " << key.GetName()
325 // << G4endl;
326 
327 #if defined (DEBUG_MEM)
328  mem_second = MemoryUsage();
329  mem_diff = mem_second-mem_first;
330  G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
331  "after creating tree, diff is : " << mem_diff << G4endl;
332 #endif
333  }
334 
335  G4TrackList* trackList = listUnion->GetMainList();
336  G4TrackList::iterator __it = trackList->begin();
337  G4TrackList::iterator __end = trackList->end();
338  // int i = 0;
339 
340  // G4cout << "Box : " << key.GetName() << G4endl;
341  // G4cout << "N tracks : " << currentBox->GetNTrack() << G4endl;
342 
343  for (; __it != __end; __it++)//, i++)
344  {
345  // G4cout << i << G4endl;
346  // G4cout << "track "
347  // << currentIT->GetTrack()->GetTrackID()
348  // << G4endl;
349  currentIT = GetIT(*__it);
350  currentNode = currentTree->Insert(currentIT);
351 
352  /*G4KDNode_Base* */
353 // currentNode = currentTree->InsertMap(currentIT);
354 // if(currentIT->GetNode())
355 // {
356 // delete currentIT->GetNode();
357  // No need: deleted when tree is cleared
358 // }
359  currentIT->SetNode(currentNode);
360 
361 #if defined (DEBUG_MEM)
362  mem_second = MemoryUsage();
363  mem_diff = mem_second-mem_first;
364  G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
365  "after currentIT->SetNode(currentNode), diff is : "
366  << mem_diff << G4endl;
367 #endif
368  }
369 
370 #if defined (DEBUG_MEM)
371  mem_second = MemoryUsage();
372  mem_diff = mem_second-mem_first;
373  G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
374  "In else{...}, diff is : " << mem_diff << G4endl;
375 #endif
376 
377  }
378 // currentTree->Build();
379  }
380 }