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()
 
   45   if (!fInstance) fInstance = new G4ITFinder();
 
   50 TEMPLATE G4ITMANAGER::G4ITFinder() : G4VITFinder()
 
   53   G4AllITFinder::Instance()->RegisterManager(this);
 
   56 TEMPLATE G4ITMANAGER::~G4ITFinder()
 
   63 void G4ITMANAGER::Clear()
 
   69     for (it = fTree.begin(); it != fTree.end(); it++)
 
   71       if (it->second) delete it->second;
 
   79 void G4ITMANAGER::iUpdatePositionMap()
 
   81   fInstance->UpdatePositionMap();
 
   85 void G4ITMANAGER::Push(G4Track* track)
 
   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
 
   92   T* aIT = T::GetMolecule(track);
 
   93   aIT->RecordCurrentPositionNTime();
 
   95   int key = aIT->GetMoleculeID();
 
   97   if (!(aIT->GetNode()))
 
   99     G4KDNode_Base* node = 0;
 
  101      TreeMap::iterator it_fTree = fTree.find(key);
 
  103     if (it_fTree != fTree.end())
 
  105       node = it_fTree->second->Insert(aIT);
 
  109       G4KDTree* aTree = new G4KDTree();
 
  110       fTree.insert(std::make_pair(key, aTree));
 
  111       node = aTree->Insert(aIT);
 
  119 G4KDTreeResultHandle G4ITMANAGER::FindNearest(const G4ThreeVector& position,
 
  122    TreeMap::iterator it = fTree.find(key->GetMoleculeID());
 
  123   if (it != fTree.end()) return it->second->Nearest(position);
 
  132 G4KDTreeResultHandle G4ITMANAGER::FindNearest(const T* point0,
 
  138     // G4cout << "Equal keys !"<< G4endl;
 
  139     G4KDNode_Base* node0 = point0->GetNode();
 
  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
 
  151     TreeMap::iterator it = fTree.find(key->GetMoleculeID());
 
  152     if (it != fTree.end())
 
  154       G4KDTreeResultHandle output(it->second->Nearest(node0));
 
  159          * G4cout << "NO OUTPUT "
 
  160          * << point0->GetName()
 
  161          * << " " << key -> GetName()
 
  166       // G4cout << "OUTPUT" << G4endl;
 
  172       // G4cout << "Pas trouve dans la map"<< key->GetName() << G4endl;
 
  179     // G4cout << "Not equal keys !"<< G4endl;
 
  180     // const G4ThreeVector& position = point0->GetTrack()->GetPosition() ;
 
  181     TreeMap::iterator it = fTree.find(key->GetMoleculeID());
 
  183     if (it != fTree.end())
 
  185       G4KDTreeResultHandle output(it->second->Nearest(*point0));
 
  188         //                G4cout << "NO OUTPUT" << G4endl;
 
  192       //                G4cout << "OUTPUT" << G4endl;
 
  198       // G4cout << "Pas trouve dans la map : "<< key->GetName() << G4endl;
 
  207 G4ITMANAGER::FindNearestInRange(const G4ThreeVector& position,
 
  211   TreeMap::iterator it = fTree.find(key->GetMoleculeID());
 
  212   if (it != fTree.end()) return it->second->NearestInRange(position, R);
 
  221 G4KDTreeResultHandle G4ITMANAGER::FindNearestInRange(const T* point0,
 
  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);
 
  237     TreeMap::iterator it = fTree.find(key->GetMoleculeID());
 
  238     if (it != fTree.end()) return it->second->NearestInRange(*point0, R);
 
  250 #include "G4MemStat.hh"
 
  251 using namespace G4MemStat;
 
  255 void G4ITMANAGER::UpdatePositionMap()
 
  257   G4KDTree* currentTree = 0;
 
  259   G4KDNode_Base* currentNode = 0;
 
  261 #if defined (DEBUG_MEM)
 
  262   MemStat mem_first, mem_second, mem_diff;
 
  265 #if defined (DEBUG_MEM)
 
  266   mem_first = MemoryUsage();
 
  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();
 
  273   TreeMap::iterator it_Tree;
 
  275   for (; it!= end; it++)
 
  279     it_Tree = fTree.find(key);
 
  281     // G4cout << "Box : " << it_Box->first.GetName() << G4endl;
 
  282     // G4cout << "N tracks : " << currentBox->GetNTrack() << G4endl;
 
  284     // G4cout << "Will update : " << it_Box->first.GetName() <<G4endl;
 
  285     if (it_Tree != fTree.end())
 
  287       currentTree = it_Tree->second;
 
  290         currentTree->Clear();
 
  291         //delete currentTree;
 
  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;
 
  303     PriorityList* listUnion=it->second;
 
  305     if(listUnion == 0 || listUnion->GetMainList() == 0
 
  306         || listUnion->GetMainList()->empty() )
 
  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;
 
  319       if (currentTree == 0)
 
  321         currentTree = new G4KDTree();
 
  322         fTree[key] = currentTree;
 
  324 //        G4cout << "**** " << "Create new tree for : " << key.GetName()
 
  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;
 
  335       G4TrackList* trackList = listUnion->GetMainList();
 
  336       G4TrackList::iterator __it = trackList->begin();
 
  337       G4TrackList::iterator __end = trackList->end();
 
  340       // G4cout << "Box : " << key.GetName() << G4endl;
 
  341       // G4cout << "N tracks : " << currentBox->GetNTrack() << G4endl;
 
  343       for (; __it != __end; __it++)//, i++)
 
  345         // G4cout << i << G4endl;
 
  346         // G4cout << "track "
 
  347         //        << currentIT->GetTrack()->GetTrackID()
 
  349         currentIT = GetIT(*__it);
 
  350         currentNode = currentTree->Insert(currentIT);
 
  353 //        currentNode = currentTree->InsertMap(currentIT);
 
  354 //        if(currentIT->GetNode())
 
  356 //          delete currentIT->GetNode();
 
  357         // No need: deleted when tree is cleared
 
  359         currentIT->SetNode(currentNode);
 
  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;
 
  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;
 
  378 //    currentTree->Build();