G4ITManager.icc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 // $Id$
00027 //
00028 // Author: Mathieu Karamitros (kara (AT) cenbg . in2p3 . fr) 
00029 //
00030 // WARNING : This class is released as a prototype.
00031 // It might strongly evolve or even disapear in the next releases.
00032 //
00033 // History:
00034 // -----------
00035 // 10 Oct 2011 M.Karamitros created
00036 //
00037 // -------------------------------------------------------------------
00038 
00039 TEMPLATE
00040 G4ITMANAGER * G4ITMANAGER::fInstance(0);
00041 
00042 TEMPLATE
00043 G4ITMANAGER * G4ITMANAGER::Instance()
00044 {
00045     if(!fInstance)
00046         fInstance = new G4ITManager();
00047 
00048     return fInstance;
00049 }
00050 
00051 TEMPLATE
00052 G4ITMANAGER::G4ITManager() : G4VITManager()
00053 {
00054     fType =  T::ITType();
00055     SetVerboseLevel(G4AllITManager::Instance()->GetVerboseLevel());
00056     G4AllITManager::Instance()->RegisterManager(this);
00057 }
00058 
00059 TEMPLATE
00060 G4ITMANAGER::~G4ITManager()
00061 {
00062     {
00063         typename BoxMap::iterator it;
00064 
00065         for(it = fBox.begin() ; it != fBox.end() ; it++)
00066         {
00067             if(it->second)
00068                 delete it->second;
00069         }
00070         fBox.clear();
00071     }
00072     {
00073 
00074         typename TreeMap::iterator it;
00075 
00076         for(it = fTree.begin() ; it != fTree.end() ; it++)
00077         {
00078             if(it->second)
00079                 delete it->second;
00080         }
00081 
00082         fTree.clear();
00083 
00084         for(it = fPrevious_tree.begin() ; it != fPrevious_tree.end() ; it++)
00085         {
00086             if(it->second)
00087                 delete it->second;
00088         }
00089 
00090         fPrevious_tree.clear();
00091     }
00092 }
00093 
00094 TEMPLATE
00095 void G4ITMANAGER::iUpdatePositionMap()
00096 {
00097     fInstance->UpdatePositionMap();
00098 }
00099 
00100 TEMPLATE
00101 void G4ITMANAGER::Push(G4Track* track)
00102 {
00103     G4IT* aIT = GetIT(track) ;
00104     aIT->RecordCurrentPositionNTime();
00105 
00106     const T& key = dynamic_cast<const T&>(*aIT);
00107 
00108     typename BoxMap::iterator it = fBox.find(key);
00109 
00110     if(it != fBox.end())
00111     {
00112         G4ITBox* theBox = it->second;
00113         theBox->Push(aIT);
00114     }
00115     else
00116     {
00117         G4ITBox * itBox = new G4ITBox();
00118         std::pair<typename BoxMap::iterator,bool> ret = fBox.insert(std::make_pair (T(key), itBox)) ;
00119 
00120         typename BoxMap::iterator box_placement = ret.first;
00121         typename BoxMap::iterator previous_placement = ret.first;
00122         previous_placement--;
00123         typename BoxMap::iterator next_placement = ret.first;
00124         next_placement++;
00125         typename BoxMap::iterator last_placement = fBox.end();
00126         last_placement--;
00127 
00128         if(box_placement != fBox.begin())
00129         {
00130             G4ITBox* previous_box = previous_placement->second;
00131             if(previous_box)
00132             {
00133                 itBox->SetPreviousBox(previous_box);
00134                 previous_box->SetNextBox(itBox);
00135             }
00136         }
00137         if(box_placement != last_placement)
00138         {
00139             G4ITBox* next_box = next_placement->second;
00140             if(next_box)
00141             {
00142                 itBox->SetNextBox(next_placement->second);
00143                 next_box->SetPreviousBox(itBox);
00144             }
00145         }
00146 
00147         itBox -> Push(aIT);
00148     }
00149 
00150     if(!(aIT->GetNode()))
00151     {
00152         G4KDNode* node =  0;
00153         G4ThreeVector position = aIT->GetTrack()->GetPosition();
00154 
00155         typename TreeMap::iterator it_fTree = fTree.find(key);
00156 
00157         if(it_fTree != fTree.end())
00158         {
00159             node=it_fTree->second->Insert(position.x(),
00160                                           position.y(),
00161                                           position.z(),
00162                                           aIT);
00163         }
00164         else
00165         {
00166             G4KDTree* aTree = new G4KDTree() ;
00167             fTree.insert(std::make_pair(T(key),aTree));
00168             node = aTree->Insert(position.x(),
00169                                          position.y(),
00170                                          position.z(),
00171                                          aIT);
00172         }
00173 
00174         aIT->SetNode(node);
00175     }
00176 
00177 }
00178 
00179 TEMPLATE
00180 void G4ITMANAGER::EraseABox(T* aIT)
00181 {
00182 //    G4cout<<"G4ITMANAGER::EraseABox : " << aIT->GetName()<<G4endl;
00183 
00184     const T& key = dynamic_cast<const T&>(*aIT);
00185     typename BoxMap::iterator it = fBox.find(key);
00186     if(it != fBox.end())
00187     {
00188         fBox.erase(it);
00189     }
00190 }
00191 
00192 TEMPLATE
00193 void G4ITMANAGER::EraseABox(G4ITBox* aStack)
00194 {
00195     typename BoxMap::iterator it;
00196 
00197     for ( it=fBox.begin() ; it != fBox.end(); it++)
00198     {
00199         if(it->second == aStack)
00200         {
00201             break;
00202         }
00203     }
00204     fBox.erase(it);
00205 }
00206 
00207 TEMPLATE
00208 G4int G4ITMANAGER::NbElements(const G4IT* aIT)
00209 {
00210     const T& key = dynamic_cast<const T&>(*aIT);
00211     typename BoxMap::iterator it = fBox.find(key);
00212     if(it != fBox.end())  return it->second->GetNTrack();
00213     return -1;
00214 }
00215 
00216 
00217 TEMPLATE
00218 G4KDTreeResultHandle G4ITMANAGER::FindNearest(const G4ThreeVector& position, const T* key)
00219 {
00220     typename TreeMap::iterator it = fTree.find(*key);
00221     if(it!= fTree.end())
00222         return it->second->Nearest(position.x(),position.y(),position.z());
00223     else
00224     {
00225         return 0;
00226     }
00227     return 0;
00228 }
00229 
00230 TEMPLATE
00231 G4KDTreeResultHandle G4ITMANAGER::FindNearest(const T* point0, const T* key)
00232 {
00233     if(*point0 == *key)
00234     {
00235         // DEBUG
00236 //        G4cout << "Equal keys !"<< G4endl;
00237         G4KDNode* node0 = point0->GetNode() ;
00238 
00239         if(node0 == 0)
00240         {
00241             G4ExceptionDescription exceptionDescription
00242             ("Bad request : no node found in the IT you are searching closest neighbourg for");
00243             G4Exception("G4ITManager::FindNearest","ITManager002",
00244                         FatalErrorInArgument,exceptionDescription);
00245             return 0; // coverity
00246         }
00247 
00248         typename TreeMap::iterator it = fTree.find(*key);
00249         if(it!= fTree.end())
00250         {
00251             G4KDTreeResultHandle output(it->second->Nearest(node0));
00252 
00253             if(!output)
00254             {
00255 //                G4cout << "NO OUTPUT " << point0->GetName() << " " << key -> GetName() << G4endl;
00256                 return 0;
00257             }
00258 //                G4cout << "OUTPUT" << G4endl;
00259            return output;
00260         }
00261         else
00262         {
00263             // DEBUG
00264 //            G4cout << "Pas trouve dans la map"<< key->GetName() << G4endl;
00265             return 0;
00266         }
00267     }
00268     else
00269     {
00270         // DEBUG
00271 //        G4cout << "Not equal keys !"<< G4endl;
00272         const G4ThreeVector& position = point0->GetTrack()->GetPosition() ;
00273         typename TreeMap::iterator it = fTree.find(*key);
00274         if(it!= fTree.end())
00275         {
00276             G4KDTreeResultHandle output(it->second->Nearest(position.x(),position.y(),position.z()));
00277             if(!output)
00278             {
00279 //                G4cout << "NO OUTPUT" << G4endl;
00280                 return 0;
00281             }
00282 
00283 //                G4cout << "OUTPUT" << G4endl;
00284             return output;
00285         }
00286         else
00287         {
00288             // DEBUG
00289 //            G4cout << "Pas trouve dans la map : "<< key->GetName() << G4endl;
00290             return 0;
00291         }
00292     }
00293     return 0;
00294 }
00295 
00296 TEMPLATE
00297 G4KDTreeResultHandle G4ITMANAGER::FindNearestInRange(const G4ThreeVector& position, const T* key, G4double R)
00298 {
00299     typename TreeMap::iterator it = fTree.find(*key);
00300     if(it!= fTree.end())
00301         return it->second->NearestInRange(position.x(),position.y(),position.z(), R);
00302     else
00303     {
00304         return 0;
00305     }
00306     return 0;
00307 }
00308 
00309 TEMPLATE
00310 G4KDTreeResultHandle G4ITMANAGER::FindNearestInRange(const T* point0, const T* key, G4double R)
00311 {
00312     if(*point0 == *key)
00313     {
00314         G4KDNode* node0 = point0->GetNode() ;
00315         typename TreeMap::iterator it = fTree.find(*key);
00316         if(it!= fTree.end())
00317             return it->second->NearestInRange(node0, R);
00318         else
00319         {
00320             return 0;
00321         }
00322     }
00323     else
00324     {
00325         const G4ThreeVector& position = point0->GetTrack()->GetPosition() ;
00326 
00327         typename TreeMap::iterator it = fTree.find(*key);
00328         if(it!= fTree.end())
00329             return it->second->NearestInRange(position.x(),position.y(),position.z(), R);
00330         else
00331         {
00332             return 0;
00333         }
00334     }
00335     return 0;
00336 }
00337 
00338 TEMPLATE
00339 void G4ITMANAGER::UpdatePositionMap()
00340 {
00341     allbox_iterator allbox(this) ;
00342 
00343     if(allbox.GetBox() == 0) return;
00344 
00345     const G4ITBox* currentBox = 0 ;
00346     const G4ITBox* buffBox = 0;
00347     G4KDTree* currentTree = 0;
00348     G4IT* currentIT = 0 ;
00349 
00350     typename TreeMap::iterator it_fTree ;
00351 
00352     if(!(fPrevious_tree.empty()))
00353     {
00354 
00355         for(it_fTree = fPrevious_tree.begin() ; it_fTree != fPrevious_tree.end() ; it_fTree ++)
00356         {
00357             if(it_fTree->second)
00358                 delete (it_fTree->second) ;
00359         }
00360 
00361         fPrevious_tree.clear();
00362     }
00363 
00364     for(allbox.begin() ; !allbox.end() ; allbox++)
00365     {
00366         buffBox = allbox.GetBox() ;
00367         currentIT = (*allbox);
00368 
00369         if(currentBox != buffBox)
00370         {
00371             currentTree = 0 ;
00372             currentBox = buffBox;
00373 
00374             const T& key = dynamic_cast<const T&>(*currentIT);
00375             it_fTree = fTree.find(key);
00376 
00377             if(it_fTree != fTree.end())
00378             {
00379                 currentTree = it_fTree ->second ;
00380                 if(currentTree)
00381                 {
00382                   fPrevious_tree[key] = currentTree ;
00383                 //  currentTree->Clear();
00384                 }
00385             }
00386 
00387             // Re-initialize fTree
00388             currentTree = new G4KDTree();
00389             fTree[key] = currentTree ;
00390         }
00391 
00392         const G4ThreeVector& position = currentIT-> GetTrack()->GetPosition();
00393 
00394         G4KDNode* node = currentTree->Insert(position.x(),
00395                                              position.y(),
00396                                              position.z(),
00397                                              currentIT);
00398         currentIT->SetNode(node);
00399     }
00400 }

Generated on Mon May 27 17:48:41 2013 for Geant4 by  doxygen 1.4.7