2 // ********************************************************************
3 // * License and Disclaimer *
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. *
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. *
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 // ********************************************************************
28 // Author: Mathieu Karamitros (kara (AT) cenbg . in2p3 . fr)
30 // WARNING : This class is released as a prototype.
31 // It might strongly evolve or even disapear in the next releases.
35 // 10 Oct 2011 M.Karamitros created
37 // -------------------------------------------------------------------
40 G4ThreadLocal G4ITMANAGER * G4ITMANAGER::fInstance(0);
43 G4ITMANAGER * G4ITMANAGER::Instance()
46 fInstance = new G4ITManager();
52 G4ITMANAGER::G4ITManager() : G4VITManager()
55 SetVerboseLevel(G4AllITManager::Instance()->GetVerboseLevel());
56 G4AllITManager::Instance()->RegisterManager(this);
60 G4ITMANAGER::~G4ITManager()
63 typename BoxMap::iterator it;
65 for(it = fBox.begin() ; it != fBox.end() ; it++)
74 typename TreeMap::iterator it;
76 for(it = fTree.begin() ; it != fTree.end() ; it++)
84 for(it = fPrevious_tree.begin() ; it != fPrevious_tree.end() ; it++)
90 fPrevious_tree.clear();
95 void G4ITMANAGER::iUpdatePositionMap()
97 fInstance->UpdatePositionMap();
101 void G4ITMANAGER::Push(G4Track* track)
103 G4IT* aIT = GetIT(track) ;
104 aIT->RecordCurrentPositionNTime();
106 const T& key = dynamic_cast<const T&>(*aIT);
108 typename BoxMap::iterator it = fBox.find(key);
112 G4ITBox* theBox = it->second;
117 G4ITBox * itBox = new G4ITBox();
118 std::pair<typename BoxMap::iterator,bool> ret = fBox.insert(std::make_pair (T(key), itBox)) ;
120 typename BoxMap::iterator box_placement = ret.first;
121 typename BoxMap::iterator last_placement = fBox.end();
124 if(box_placement != fBox.begin())
126 typename BoxMap::iterator previous_placement = ret.first;
127 previous_placement--;
128 G4ITBox* previous_box = previous_placement->second;
131 itBox->SetPreviousBox(previous_box);
132 previous_box->SetNextBox(itBox);
135 if(box_placement != last_placement)
137 typename BoxMap::iterator next_placement = ret.first;
139 G4ITBox* next_box = next_placement->second;
142 itBox->SetNextBox(next_placement->second);
143 next_box->SetPreviousBox(itBox);
150 if(!(aIT->GetNode()))
153 G4ThreeVector position = aIT->GetTrack()->GetPosition();
155 typename TreeMap::iterator it_fTree = fTree.find(key);
157 if(it_fTree != fTree.end())
159 node=it_fTree->second->Insert(position.x(),
166 G4KDTree* aTree = new G4KDTree() ;
167 fTree.insert(std::make_pair(T(key),aTree));
168 node = aTree->Insert(position.x(),
180 void G4ITMANAGER::EraseABox(T* aIT)
182 // G4cout<<"G4ITMANAGER::EraseABox : " << aIT->GetName()<<G4endl;
184 const T& key = dynamic_cast<const T&>(*aIT);
185 typename BoxMap::iterator it = fBox.find(key);
193 void G4ITMANAGER::EraseABox(G4ITBox* aStack)
195 typename BoxMap::iterator it;
197 for ( it=fBox.begin() ; it != fBox.end(); it++)
199 if(it->second == aStack)
208 G4int G4ITMANAGER::NbElements(const G4IT* aIT)
210 const T& key = dynamic_cast<const T&>(*aIT);
211 typename BoxMap::iterator it = fBox.find(key);
212 if(it != fBox.end()) return it->second->GetNTrack();
218 G4KDTreeResultHandle G4ITMANAGER::FindNearest(const G4ThreeVector& position, const T* key)
220 typename TreeMap::iterator it = fTree.find(*key);
222 return it->second->Nearest(position.x(),position.y(),position.z());
231 G4KDTreeResultHandle G4ITMANAGER::FindNearest(const T* point0, const T* key)
236 // G4cout << "Equal keys !"<< G4endl;
237 G4KDNode* node0 = point0->GetNode() ;
241 G4ExceptionDescription exceptionDescription
242 ("Bad request : no node found in the IT you are searching closest neighbourg for");
243 G4Exception("G4ITManager::FindNearest","ITManager002",
244 FatalErrorInArgument,exceptionDescription);
245 return 0; // coverity
248 typename TreeMap::iterator it = fTree.find(*key);
251 G4KDTreeResultHandle output(it->second->Nearest(node0));
255 // G4cout << "NO OUTPUT " << point0->GetName() << " " << key -> GetName() << G4endl;
258 // G4cout << "OUTPUT" << G4endl;
264 // G4cout << "Pas trouve dans la map"<< key->GetName() << G4endl;
271 // G4cout << "Not equal keys !"<< G4endl;
272 const G4ThreeVector& position = point0->GetTrack()->GetPosition() ;
273 typename TreeMap::iterator it = fTree.find(*key);
276 G4KDTreeResultHandle output(it->second->Nearest(position.x(),position.y(),position.z()));
279 // G4cout << "NO OUTPUT" << G4endl;
283 // G4cout << "OUTPUT" << G4endl;
289 // G4cout << "Pas trouve dans la map : "<< key->GetName() << G4endl;
297 G4KDTreeResultHandle G4ITMANAGER::FindNearestInRange(const G4ThreeVector& position, const T* key, G4double R)
299 typename TreeMap::iterator it = fTree.find(*key);
301 return it->second->NearestInRange(position.x(),position.y(),position.z(), R);
310 G4KDTreeResultHandle G4ITMANAGER::FindNearestInRange(const T* point0, const T* key, G4double R)
314 G4KDNode* node0 = point0->GetNode() ;
315 typename TreeMap::iterator it = fTree.find(*key);
317 return it->second->NearestInRange(node0, R);
325 const G4ThreeVector& position = point0->GetTrack()->GetPosition() ;
327 typename TreeMap::iterator it = fTree.find(*key);
329 return it->second->NearestInRange(position.x(),position.y(),position.z(), R);
339 void G4ITMANAGER::UpdatePositionMap()
341 allbox_iterator allbox(this) ;
343 if(allbox.GetBox() == 0) return;
345 const G4ITBox* currentBox = 0 ;
346 const G4ITBox* buffBox = 0;
347 G4KDTree* currentTree = 0;
348 G4IT* currentIT = 0 ;
350 typename TreeMap::iterator it_fTree ;
352 if(!(fPrevious_tree.empty()))
355 for(it_fTree = fPrevious_tree.begin() ; it_fTree != fPrevious_tree.end() ; it_fTree ++)
358 delete (it_fTree->second) ;
361 fPrevious_tree.clear();
364 for(allbox.begin() ; !allbox.end() ; allbox++)
366 buffBox = allbox.GetBox() ;
367 currentIT = (*allbox);
369 if(currentBox != buffBox)
372 currentBox = buffBox;
374 const T& key = dynamic_cast<const T&>(*currentIT);
375 it_fTree = fTree.find(key);
377 if(it_fTree != fTree.end())
379 currentTree = it_fTree ->second ;
382 fPrevious_tree[key] = currentTree ;
383 // currentTree->Clear();
387 // Re-initialize fTree
388 currentTree = new G4KDTree();
389 fTree[key] = currentTree ;
392 const G4ThreeVector& position = currentIT-> GetTrack()->GetPosition();
394 G4KDNode* node = currentTree->Insert(position.x(),
399 // G4KDNode* node = currentTree->InsertMap(position.x(),
404 currentIT->SetNode(node);
407 // currentTree->Build();