Import Geant4 10.4.0.beta source tree
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
$Id: History 101678 2016-11-21 09:28:32Z gcosmo $
|
||||
$Id: History 103976 2017-05-05 12:22:53Z gcosmo $
|
||||
-------------------------------------------------------------------
|
||||
|
||||
=========================================================
|
||||
@@ -17,6 +17,47 @@ committal in the CVS repository !
|
||||
* Reverse chronological order (last date on top), please *
|
||||
----------------------------------------------------------
|
||||
|
||||
May 2, 2017 J. Madsen (digits_hits-V10-03-01)
|
||||
- Update to G4THitsMap to fix issue where G4Colour was
|
||||
using G4THitsMap<G4StatDouble> and the allocation
|
||||
was causing += to white instead of += to black
|
||||
|
||||
April 20, 2017 J. Madsen (digit_hits-V10-03-00)
|
||||
- Files modified : hits/include/G4THitsMap.hh
|
||||
- Extension of the G4THitsMap class + bug fix
|
||||
- Renamed G4THitsMap to G4VTHitsMap
|
||||
- although G4VTHitsMap is not a pure virtual class
|
||||
it has an additional template parameter for the map
|
||||
type, so it is not intended to be used directly
|
||||
- Made G4VTHitsMap more generic in accepting the type
|
||||
of data stored (with more generic template overloads not
|
||||
specific to G4double and G4StatDouble)
|
||||
- Fixed a bug in initializing G4StatDouble entries
|
||||
- G4StatDouble(0) is not the same as G4StatDouble().
|
||||
The former starts off with a count = 1, and the
|
||||
latter starts with a count = 0.
|
||||
- Added G4THitsMap
|
||||
- deriving from G4VTHitsMap and having the same
|
||||
functionality as before
|
||||
- Added G4THitsMultiMap
|
||||
- which functions like G4THitsMap but all entries are
|
||||
stored
|
||||
- Added G4THitsUnorderedMap
|
||||
- which functions like G4THitsMap but uses an
|
||||
unordered_map
|
||||
- Added G4THitsUnorderedMultiMap
|
||||
- which functions like G4THitsMap but stores all
|
||||
entries in an unordered multimap
|
||||
- typedef for map_type, value_type, iterator, and const_iterator
|
||||
for future generic usage
|
||||
- e.g. replacing
|
||||
std::map<G4int, G4double*> theMap = this->GetMap()
|
||||
with
|
||||
map_type theMap = this->GetMap()
|
||||
- defined begin(), end(), cbegin(), cend() so now
|
||||
G4VTHitsMap can be used in range-based loops
|
||||
- e.g. for(auto itr : hitMap) { ... }
|
||||
|
||||
Nov 15, 2016, A.Dotti (digits_hits-V10-02-09,-10)
|
||||
- Fix bug #1908, remove implicit addition to manager of
|
||||
SD passed via G4MultiSD proxy
|
||||
|
||||
@@ -24,14 +24,16 @@
|
||||
// ********************************************************************
|
||||
//
|
||||
//
|
||||
// $Id: G4THitsMap.hh 99262 2016-09-09 13:18:19Z gcosmo $
|
||||
// $Id: G4THitsMap.hh 103976 2017-05-05 12:22:53Z gcosmo $
|
||||
//
|
||||
#ifndef G4THitsMap_h
|
||||
#define G4THitsMap_h 1
|
||||
|
||||
#include "G4THitsCollection.hh"
|
||||
#include "globals.hh"
|
||||
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
|
||||
class G4StatDouble;
|
||||
|
||||
@@ -45,235 +47,606 @@ class G4StatDouble;
|
||||
// cannot be instansiated with a template class. Thus G4HitsMap
|
||||
// class MUST NOT be directly used by the user.
|
||||
|
||||
template <typename T> class G4THitsMap : public G4HitsCollection
|
||||
template <typename T, typename Map_t = std::map<G4int, T*> >
|
||||
class G4VTHitsMap : public G4HitsCollection
|
||||
{
|
||||
public:
|
||||
G4THitsMap();
|
||||
public: // with description
|
||||
G4THitsMap(G4String detName,G4String colNam);
|
||||
// constructor.
|
||||
public:
|
||||
virtual ~G4THitsMap();
|
||||
G4int operator==(const G4THitsMap<T> &right) const;
|
||||
private:
|
||||
typedef std::multimap<G4int, T*> mmap_t;
|
||||
typedef std::pair<G4int, T*> pair_t;
|
||||
typedef std::unordered_multimap<G4int, T*> uommap_t;
|
||||
|
||||
public: // with description
|
||||
// Operator += between same kind of classes
|
||||
template <typename U = T,
|
||||
typename std::enable_if<std::is_same<U,T>::value,int>::type=0>
|
||||
G4THitsMap<T> & operator+=(const G4THitsMap<T> &right) const
|
||||
{
|
||||
std::map<G4int,T*> * aHitsMap = right.GetMap();
|
||||
typename std::map<G4int,T*>::iterator itr = aHitsMap->begin();
|
||||
for(; itr != aHitsMap->end(); itr++) {
|
||||
add(itr->first, *(itr->second));
|
||||
}
|
||||
return (G4THitsMap<T>&)(*this);
|
||||
}
|
||||
// Operator for G4THitsMap<G4StatDouble> += G4THitsMap<G4double>
|
||||
template <typename U = T,
|
||||
typename std::enable_if<std::is_same<U,G4double>::value &&
|
||||
std::is_same<T,G4StatDouble>::value,int>::type=0>
|
||||
G4THitsMap<T> & operator+=(const G4THitsMap<U> &right) const
|
||||
{
|
||||
std::map<G4int,U*> * aHitsMap = right.GetMap();
|
||||
typename std::map<G4int,U*>::iterator itr = aHitsMap->begin();
|
||||
for(; itr != aHitsMap->end(); itr++) {
|
||||
typename std::map<G4int,T*>::iterator mapItr = this->GetMap()->find(itr->first);
|
||||
if(mapItr==this->GetMap()->end())
|
||||
{ (*this->GetMap())[itr->first] = new T(0.); }
|
||||
add(itr->first, *(itr->second));
|
||||
}
|
||||
return (G4THitsMap<T>&)(*this);
|
||||
}
|
||||
#define is_same_t(_Tp, _Up) std::is_same<_Tp, _Up>::value
|
||||
#define is_multimap_t(_Mp) std::is_same<_Mp, mmap_t>::value
|
||||
#define is_uommap_t(_Mp) std::is_same<_Mp, uommap_t>::value
|
||||
#define is_mmap_t(_Mp) (is_multimap_t(_Mp) || is_uommap_t(_Mp))
|
||||
#define is_fundamental_t(_Tp) std::is_fundamental<_Tp>::value
|
||||
|
||||
public: // with description
|
||||
virtual void DrawAllHits();
|
||||
virtual void PrintAllHits();
|
||||
// These two methods invokes Draw() and Print() methods of all of
|
||||
// hit objects stored in this map, respectively.
|
||||
template <bool _Bp, typename _Tp = void>
|
||||
using enable_if_t = typename std::enable_if<_Bp, _Tp>::type;
|
||||
|
||||
public: // with description
|
||||
// Returns a pointer to a concrete hit object.
|
||||
inline T* operator[](G4int key) const;
|
||||
// Returns a collection map.
|
||||
inline std::map<G4int,T*>* GetMap() const
|
||||
{ return (std::map<G4int,T*>*)theCollection; }
|
||||
// ensure fundamental types are initialized to zero
|
||||
template <typename U = T, enable_if_t<is_fundamental_t(U), int> = 0>
|
||||
T* allocate() const
|
||||
{ return new T(0.); }
|
||||
// non-fundamental types should set values to appropriate values
|
||||
// and avoid issues such as:
|
||||
// G4StatDouble stat(0.); stat += 1.0; gives n == 2;
|
||||
template <typename U = T, enable_if_t<! is_fundamental_t(U), int> = 0>
|
||||
T* allocate() const
|
||||
{ return new T(); }
|
||||
|
||||
// Insert a hit object. Total number of hit objects stored in this
|
||||
// map is returned.
|
||||
template <typename U = T,
|
||||
typename std::enable_if<std::is_same<U,T>::value,int>::type=0>
|
||||
inline G4int add(const G4int & key, T * &aHit) const
|
||||
{
|
||||
typename std::map<G4int,T*> * theHitsMap = GetMap();
|
||||
if(theHitsMap->find(key) != theHitsMap->end()) {
|
||||
*(*theHitsMap)[key] += *aHit;
|
||||
} else {
|
||||
(*theHitsMap)[key] = aHit;
|
||||
}
|
||||
return theHitsMap->size();
|
||||
}
|
||||
template <typename U = T,
|
||||
typename std::enable_if<std::is_same<U,T>::value,int>::type=0>
|
||||
inline G4int add(const G4int & key, T &aHit) const
|
||||
{
|
||||
typename std::map<G4int,T*> * theHitsMap = GetMap();
|
||||
if(theHitsMap->find(key) == theHitsMap->end()) {
|
||||
(*theHitsMap)[key] = new T(0.);
|
||||
}
|
||||
*(*theHitsMap)[key] += aHit;
|
||||
return theHitsMap->size();
|
||||
}
|
||||
template <typename U = T,
|
||||
typename std::enable_if<std::is_same<U,G4double>::value &&
|
||||
std::is_same<T,G4StatDouble>::value,int>::type=0>
|
||||
inline G4int add(const G4int & key, U * &aHit) const
|
||||
{
|
||||
typename std::map<G4int,T*> * theHitsMap = GetMap();
|
||||
if(theHitsMap->find(key) == theHitsMap->end()) {
|
||||
(*theHitsMap)[key] = new T(0.);
|
||||
}
|
||||
*(*theHitsMap)[key] += *aHit;
|
||||
return theHitsMap->size();
|
||||
}
|
||||
template <typename U = T,
|
||||
typename std::enable_if<std::is_same<U,G4double>::value &&
|
||||
std::is_same<T,G4StatDouble>::value,int>::type=0>
|
||||
inline G4int add(const G4int & key, U &aHit) const
|
||||
{
|
||||
typename std::map<G4int,T*> * theHitsMap = GetMap();
|
||||
if(theHitsMap->find(key) == theHitsMap->end()) {
|
||||
(*theHitsMap)[key] = new T(0.);
|
||||
}
|
||||
*(*theHitsMap)[key] += aHit;
|
||||
return theHitsMap->size();
|
||||
}
|
||||
public:
|
||||
typedef G4VTHitsMap<T, Map_t> this_type;
|
||||
typedef T value_type;
|
||||
typedef Map_t map_type;
|
||||
typedef typename map_type::iterator iterator;
|
||||
typedef typename map_type::const_iterator const_iterator;
|
||||
|
||||
// Overwrite a hit object. Total number of hit objects stored in this
|
||||
// map is returned.
|
||||
template <typename U = T,
|
||||
typename std::enable_if<std::is_same<U,T>::value,int>::type=0>
|
||||
inline G4int set(const G4int & key, T * &aHit) const
|
||||
{
|
||||
typename std::map<G4int,T*> * theHitsMap = GetMap();
|
||||
if(theHitsMap->find(key) != theHitsMap->end()) {
|
||||
delete (*theHitsMap)[key]->second;
|
||||
}
|
||||
(*theHitsMap)[key] = aHit;
|
||||
return theHitsMap->size();
|
||||
}
|
||||
template <typename U = T,
|
||||
typename std::enable_if<std::is_same<U,T>::value,int>::type=0>
|
||||
inline G4int set(const G4int & key, T &aHit) const
|
||||
{
|
||||
typename std::map<G4int,T*> * theHitsMap = GetMap();
|
||||
if(theHitsMap->find(key) == theHitsMap->end())
|
||||
{ (*theHitsMap)[key] = new T(0.); }
|
||||
*(*theHitsMap)[key] = aHit;
|
||||
return theHitsMap->size();
|
||||
}
|
||||
template <typename U = T,
|
||||
typename std::enable_if<std::is_same<U,G4double>::value &&
|
||||
std::is_same<T,G4StatDouble>::value,int>::type=0>
|
||||
inline G4int set(const G4int & key, U * &aHit) const
|
||||
{
|
||||
typename std::map<G4int,T*> * theHitsMap = GetMap();
|
||||
if(theHitsMap->find(key) != theHitsMap->end()) {
|
||||
delete (*theHitsMap)[key]->second;
|
||||
}
|
||||
(*theHitsMap)[key] = aHit;
|
||||
return theHitsMap->size();
|
||||
}
|
||||
template <typename U = T,
|
||||
typename std::enable_if<std::is_same<U,G4double>::value &&
|
||||
std::is_same<T,G4StatDouble>::value,int>::type=0>
|
||||
inline G4int set(const G4int & key, U &aHit) const
|
||||
{
|
||||
typename std::map<G4int,T*> * theHitsMap = GetMap();
|
||||
if(theHitsMap->find(key) == theHitsMap->end())
|
||||
{ (*theHitsMap)[key] = new T(0.); }
|
||||
*(*theHitsMap)[key] = aHit;
|
||||
return theHitsMap->size();
|
||||
}
|
||||
public: // with description
|
||||
// generic constructor
|
||||
G4VTHitsMap();
|
||||
// det + collection description constructor
|
||||
G4VTHitsMap(G4String detName, G4String colNam);
|
||||
// destructor
|
||||
virtual ~G4VTHitsMap();
|
||||
// equivalence operator
|
||||
G4int operator==(const G4VTHitsMap<T, Map_t> &right) const;
|
||||
|
||||
// Returns the number of hit objects stored in this map
|
||||
inline G4int entries() const
|
||||
{ return ((std::map<G4int,T*>*)theCollection)->size(); }
|
||||
//------------------------------------------------------------------------//
|
||||
// Generic operator += where add(...) overloads handle various
|
||||
// U and MapU_t types
|
||||
//------------------------------------------------------------------------//
|
||||
template <typename U, typename MapU_t>
|
||||
this_type& operator+=(const G4VTHitsMap<U, MapU_t>& right) const
|
||||
{
|
||||
MapU_t* aHitsMap = right.GetMap();
|
||||
for(auto itr = aHitsMap->begin(); itr != aHitsMap->end(); itr++)
|
||||
add<U, map_type>(itr->first, *(itr->second));
|
||||
return (this_type&)(*this);
|
||||
}
|
||||
//------------------------------------------------------------------------//
|
||||
|
||||
// Clear the entry
|
||||
inline void clear();
|
||||
public: // with description
|
||||
virtual void DrawAllHits();
|
||||
virtual void PrintAllHits();
|
||||
// These two methods invokes Draw() and Print() methods of all of
|
||||
// hit objects stored in this map, respectively.
|
||||
|
||||
public:
|
||||
public: // with description
|
||||
// Returns a pointer to a concrete hit object.
|
||||
inline Map_t* GetMap() const
|
||||
{ return (Map_t*)theCollection; }
|
||||
// Overwrite a hit object. Total number of hit objects stored in this
|
||||
// map is returned.
|
||||
inline G4int entries() const
|
||||
{ return ((Map_t*)theCollection)->size(); }
|
||||
// Returns the number of hit objects stored in this map
|
||||
inline void clear();
|
||||
|
||||
iterator begin() { return GetMap()->begin(); }
|
||||
iterator end() { return GetMap()->end(); }
|
||||
const_iterator begin() const { return GetMap()->begin(); }
|
||||
const_iterator end() const { return GetMap()->end(); }
|
||||
const_iterator cbegin() const { return GetMap()->cbegin(); }
|
||||
const_iterator cend() const { return GetMap()->cend(); }
|
||||
|
||||
public:
|
||||
virtual G4VHit* GetHit(size_t) const {return 0;}
|
||||
virtual size_t GetSize() const
|
||||
{ return ((std::map<G4int,T*>*)theCollection)->size(); }
|
||||
{ return ((Map_t*)theCollection)->size(); }
|
||||
|
||||
public:
|
||||
//------------------------------------------------------------------------//
|
||||
// Add/Insert a hit object. Total number of hit objects stored in this
|
||||
// map is returned.
|
||||
//------------------------------------------------------------------------//
|
||||
// Standard map overload for same type
|
||||
//------------------------------------------------------------------------//
|
||||
// here we don't use allocate() since instances like G4Colour() == white
|
||||
// and += adds to white (not correct)
|
||||
template <typename U = T,
|
||||
typename MapU_t = Map_t,
|
||||
enable_if_t<is_same_t(U, T) && ! is_mmap_t(MapU_t), int> = 0>
|
||||
G4int add(const G4int& key, U*& aHit) const
|
||||
{
|
||||
map_type* theHitsMap = GetMap();
|
||||
if(theHitsMap->find(key) == theHitsMap->end())
|
||||
theHitsMap->insert(pair_t(key, new T(*aHit)));
|
||||
else
|
||||
*theHitsMap->find(key)->second += *aHit;
|
||||
return theHitsMap->size();
|
||||
}
|
||||
//------------------------------------------------------------------------//
|
||||
// Multimap overload for same type T
|
||||
//------------------------------------------------------------------------//
|
||||
template <typename U = T,
|
||||
typename MapU_t = Map_t,
|
||||
enable_if_t<(is_same_t(U, T) && is_mmap_t(MapU_t)), int> = 0>
|
||||
G4int add(const G4int& key, U*& aHit) const
|
||||
{
|
||||
map_type* theHitsMap = GetMap();
|
||||
theHitsMap->insert(pair_t(key, aHit));
|
||||
return theHitsMap->size();
|
||||
}
|
||||
//------------------------------------------------------------------------//
|
||||
// Multimap overload for different types
|
||||
// assumes type T has overload of += operator for U
|
||||
//------------------------------------------------------------------------//
|
||||
template <typename U = T,
|
||||
typename MapU_t = Map_t,
|
||||
enable_if_t<(!is_same_t(U, T) && is_mmap_t(MapU_t)), int> = 0>
|
||||
G4int add(const G4int& key, U*& aHit) const
|
||||
{
|
||||
map_type* theHitsMap = GetMap();
|
||||
T* hit = allocate();
|
||||
*hit += *aHit;
|
||||
theHitsMap->insert(pair_t(key, hit));
|
||||
return theHitsMap->size();
|
||||
}
|
||||
|
||||
public:
|
||||
//------------------------------------------------------------------------//
|
||||
// Standard map overload for same type
|
||||
// assumes type T has overload of += operator for U
|
||||
//------------------------------------------------------------------------//
|
||||
// here we don't use allocate() since instances like G4Colour() == white
|
||||
// and += adds to white (not correct)
|
||||
template <typename U = T,
|
||||
typename MapU_t = Map_t,
|
||||
enable_if_t<(is_same_t(U, T) && ! is_mmap_t(MapU_t)), int> = 0>
|
||||
G4int add(const G4int& key, U& aHit) const
|
||||
{
|
||||
map_type* theHitsMap = GetMap();
|
||||
if(theHitsMap->find(key) == theHitsMap->end())
|
||||
theHitsMap->insert(pair_t(key, new T(aHit)));
|
||||
else
|
||||
*theHitsMap->find(key)->second += aHit;
|
||||
return theHitsMap->size();
|
||||
}
|
||||
//------------------------------------------------------------------------//
|
||||
// Standard map overload for different type
|
||||
// assumes type T has overload of += operator for U
|
||||
//------------------------------------------------------------------------//
|
||||
template <typename U = T,
|
||||
typename MapU_t = Map_t,
|
||||
enable_if_t<(! is_same_t(U, T) && ! is_mmap_t(MapU_t)), int> = 0>
|
||||
G4int add(const G4int& key, U& aHit) const
|
||||
{
|
||||
map_type* theHitsMap = GetMap();
|
||||
if(theHitsMap->find(key) == theHitsMap->end())
|
||||
theHitsMap->insert(pair_t(key, allocate()));
|
||||
*theHitsMap->find(key)->second += aHit;
|
||||
return theHitsMap->size();
|
||||
}
|
||||
//------------------------------------------------------------------------//
|
||||
// Multimap overload for same type T
|
||||
//------------------------------------------------------------------------//
|
||||
template <typename U = T,
|
||||
typename MapU_t = Map_t,
|
||||
enable_if_t<(is_same_t(U, T) && is_mmap_t(MapU_t)), int> = 0>
|
||||
G4int add(const G4int& key, U& aHit) const
|
||||
{
|
||||
map_type* theHitsMap = GetMap();
|
||||
theHitsMap->insert(pair_t(key, new T(aHit)));
|
||||
return theHitsMap->size();
|
||||
}
|
||||
//------------------------------------------------------------------------//
|
||||
// Multimap overload for different types
|
||||
// assumes type T has overload of += operator for U
|
||||
//------------------------------------------------------------------------//
|
||||
template <typename U = T,
|
||||
typename MapU_t = Map_t,
|
||||
enable_if_t<(!is_same_t(U, T) && is_mmap_t(MapU_t)), int> = 0>
|
||||
G4int add(const G4int& key, U& aHit) const
|
||||
{
|
||||
map_type* theHitsMap = GetMap();
|
||||
T* hit = allocate();
|
||||
*hit += aHit;
|
||||
theHitsMap->insert(pair_t(key, hit));
|
||||
return theHitsMap->size();
|
||||
}
|
||||
//------------------------------------------------------------------------//
|
||||
|
||||
public:
|
||||
//------------------------------------------------------------------------//
|
||||
// Set a hit object. Total number of hit objects stored in this
|
||||
// map is returned.
|
||||
//------------------------------------------------------------------------//
|
||||
// Standard overload for same type T
|
||||
//------------------------------------------------------------------------//
|
||||
template <typename U = T,
|
||||
typename MapU_t = Map_t,
|
||||
enable_if_t<(is_same_t(U, T) && ! is_mmap_t(MapU_t)), int> = 0>
|
||||
inline G4int set(const G4int& key, U*& aHit) const
|
||||
{
|
||||
map_type* theHitsMap = GetMap();
|
||||
if(theHitsMap->find(key) != theHitsMap->end())
|
||||
delete theHitsMap->find(key)->second;
|
||||
theHitsMap->find(key)->second = aHit;
|
||||
return theHitsMap->size();
|
||||
}
|
||||
//------------------------------------------------------------------------//
|
||||
// Multimap overload for same type T
|
||||
//------------------------------------------------------------------------//
|
||||
template <typename U = T,
|
||||
typename MapU_t = Map_t,
|
||||
enable_if_t<(is_same_t(U, T) && is_mmap_t(MapU_t)), int> = 0>
|
||||
inline G4int set(const G4int& key, U*& aHit) const
|
||||
{
|
||||
map_type* theHitsMap = GetMap();
|
||||
if(theHitsMap->find(key) != theHitsMap->end())
|
||||
theHitsMap->insert(pair_t(key, aHit));
|
||||
else
|
||||
{
|
||||
delete theHitsMap->find(key)->second;
|
||||
theHitsMap->find(key)->second = aHit;
|
||||
}
|
||||
return theHitsMap->size();
|
||||
}
|
||||
//------------------------------------------------------------------------//
|
||||
// Standard map overload for different types
|
||||
//------------------------------------------------------------------------//
|
||||
template <typename U = T,
|
||||
typename MapU_t = Map_t,
|
||||
enable_if_t<(!is_same_t(U, T) && ! is_mmap_t(MapU_t)), int> = 0>
|
||||
inline G4int set(const G4int & key, U*& aHit) const
|
||||
{
|
||||
map_type* theHitsMap = GetMap();
|
||||
T* hit = nullptr;
|
||||
if(theHitsMap->find(key) == theHitsMap->end())
|
||||
theHitsMap->insert(std::make_pair(key, hit = allocate()));
|
||||
else
|
||||
hit = theHitsMap->find(key)->second;
|
||||
*hit += *aHit;
|
||||
return theHitsMap->size();
|
||||
}
|
||||
//------------------------------------------------------------------------//
|
||||
// Multimap overload for different types
|
||||
//------------------------------------------------------------------------//
|
||||
template <typename U = T,
|
||||
typename MapU_t = Map_t,
|
||||
enable_if_t<(!is_same_t(U, T) && is_mmap_t(MapU_t)), int> = 0>
|
||||
inline G4int set(const G4int& key, U*& aHit) const
|
||||
{
|
||||
map_type* theHitsMap = GetMap();
|
||||
T* hit = allocate();
|
||||
*hit += *aHit;
|
||||
if(theHitsMap->find(key) != theHitsMap->end())
|
||||
theHitsMap->insert(pair_t(key, hit));
|
||||
else
|
||||
{
|
||||
delete theHitsMap->find(key)->second;
|
||||
theHitsMap->find(key)->second = hit;
|
||||
}
|
||||
return theHitsMap->size();
|
||||
}
|
||||
//------------------------------------------------------------------------//
|
||||
|
||||
public:
|
||||
//------------------------------------------------------------------------//
|
||||
// Set a hit object. Total number of hit objects stored in this
|
||||
// map is returned.
|
||||
//------------------------------------------------------------------------//
|
||||
// Standard overload for same type T
|
||||
//------------------------------------------------------------------------//
|
||||
template <typename U = T,
|
||||
typename MapU_t = Map_t,
|
||||
enable_if_t<(is_same_t(U, T) && ! is_mmap_t(MapU_t)), int> = 0>
|
||||
inline G4int set(const G4int& key, U& aHit) const
|
||||
{
|
||||
map_type* theHitsMap = GetMap();
|
||||
T* hit = nullptr;
|
||||
if(theHitsMap->find(key) != theHitsMap->end())
|
||||
hit = theHitsMap->find(key)->second;
|
||||
else
|
||||
theHitsMap->insert(pair_t(key, hit = allocate()));
|
||||
*hit = aHit;
|
||||
return theHitsMap->size();
|
||||
}
|
||||
//------------------------------------------------------------------------//
|
||||
// Multimap overload for same type T
|
||||
//------------------------------------------------------------------------//
|
||||
template <typename U = T,
|
||||
typename MapU_t = Map_t,
|
||||
enable_if_t<(is_same_t(U, T) && is_mmap_t(MapU_t)), int> = 0>
|
||||
inline G4int set(const G4int& key, U& aHit) const
|
||||
{
|
||||
map_type* theHitsMap = GetMap();
|
||||
if(theHitsMap->find(key) != theHitsMap->end())
|
||||
*theHitsMap->find(key)->second = aHit;
|
||||
else
|
||||
theHitsMap->insert(pair_t(key, new T(aHit)));
|
||||
return theHitsMap->size();
|
||||
}
|
||||
//------------------------------------------------------------------------//
|
||||
// Standard map overload for different types
|
||||
//------------------------------------------------------------------------//
|
||||
template <typename U = T,
|
||||
typename MapU_t = Map_t,
|
||||
enable_if_t<(!is_same_t(U, T) && ! is_mmap_t(MapU_t)), int> = 0>
|
||||
inline G4int set(const G4int & key, U& aHit) const
|
||||
{
|
||||
map_type* theHitsMap = GetMap();
|
||||
T* hit = nullptr;
|
||||
if(theHitsMap->find(key) == theHitsMap->end())
|
||||
theHitsMap->insert(std::make_pair(key, hit = allocate()));
|
||||
else
|
||||
hit = theHitsMap->find(key)->second;
|
||||
*hit += aHit;
|
||||
return theHitsMap->size();
|
||||
}
|
||||
//------------------------------------------------------------------------//
|
||||
// Multimap overload for different types
|
||||
//------------------------------------------------------------------------//
|
||||
template <typename U = T,
|
||||
typename MapU_t = Map_t,
|
||||
enable_if_t<(!is_same_t(U, T) && is_mmap_t(MapU_t)), int> = 0>
|
||||
inline G4int set(const G4int& key, U& aHit) const
|
||||
{
|
||||
map_type* theHitsMap = GetMap();
|
||||
T* hit = allocate();
|
||||
*hit += aHit;
|
||||
if(theHitsMap->find(key) != theHitsMap->end())
|
||||
*theHitsMap->find(key)->second = *hit;
|
||||
else
|
||||
theHitsMap->insert(pair_t(key, hit));
|
||||
return theHitsMap->size();
|
||||
}
|
||||
//------------------------------------------------------------------------//
|
||||
|
||||
public:
|
||||
//------------------------------------------------------------------------//
|
||||
// Enable bracket operator. Return pointer to data indexed by key or
|
||||
// last occurring instance of pointer to data index by key in the
|
||||
// case of a multimap
|
||||
//------------------------------------------------------------------------//
|
||||
template <typename MapU_t = Map_t,
|
||||
enable_if_t<! is_mmap_t(MapU_t), int> = 0>
|
||||
T* operator[](G4int key) const
|
||||
{
|
||||
map_type* theHitsMap = GetMap();
|
||||
if(theHitsMap->find(key) != theHitsMap->end())
|
||||
return theHitsMap->find(key)->second;
|
||||
return nullptr;
|
||||
}
|
||||
//------------------------------------------------------------------------//
|
||||
template <typename MapU_t = Map_t,
|
||||
enable_if_t<is_mmap_t(MapU_t), int> = 0>
|
||||
T* operator[](G4int key) const
|
||||
{
|
||||
#ifdef G4VERBOSE
|
||||
static bool _first = true;
|
||||
if(_first)
|
||||
{
|
||||
_first = false;
|
||||
G4Exception("G4THitsMap operator[]", "calling [] on multimap",
|
||||
JustWarning, "Returning the last matching entry");
|
||||
}
|
||||
#endif
|
||||
map_type* theHitsMap = GetMap();
|
||||
iterator itr = theHitsMap->find(key);
|
||||
if(itr != theHitsMap->end())
|
||||
{
|
||||
std::advance(itr, theHitsMap->count(key)-1);
|
||||
return itr->second;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
//------------------------------------------------------------------------//
|
||||
|
||||
#undef is_same_t
|
||||
#undef is_multimap_t
|
||||
#undef is_uommap_t
|
||||
#undef is_mmap_t
|
||||
#undef is_fundamental_t
|
||||
};
|
||||
|
||||
template <typename T> G4THitsMap<T>::G4THitsMap()
|
||||
{
|
||||
theCollection = (void*)new std::map<G4int,T*>;
|
||||
}
|
||||
//============================================================================//
|
||||
|
||||
template <typename T> G4THitsMap<T>::G4THitsMap(G4String detName,G4String colNam)
|
||||
: G4HitsCollection(detName,colNam)
|
||||
{
|
||||
theCollection = (void*)new std::map<G4int,T*>;
|
||||
}
|
||||
|
||||
template <typename T> G4THitsMap<T>::~G4THitsMap()
|
||||
template <typename T, typename Map_t>
|
||||
G4VTHitsMap<T, Map_t>::G4VTHitsMap()
|
||||
{
|
||||
typename std::map<G4int,T*> * theHitsMap = GetMap();
|
||||
typename std::map<G4int,T*>::iterator itr = theHitsMap->begin();
|
||||
for(; itr != theHitsMap->end(); itr++) {
|
||||
delete itr->second;
|
||||
}
|
||||
theCollection = (void*)new Map_t;
|
||||
}
|
||||
|
||||
//============================================================================//
|
||||
|
||||
template <typename T, typename Map_t>
|
||||
G4VTHitsMap<T, Map_t>::G4VTHitsMap(G4String detName,G4String colNam)
|
||||
: G4HitsCollection(detName,colNam)
|
||||
{
|
||||
theCollection = (void*)new Map_t;
|
||||
}
|
||||
|
||||
//============================================================================//
|
||||
|
||||
template <typename T, typename Map_t>
|
||||
G4VTHitsMap<T, Map_t>::~G4VTHitsMap()
|
||||
{
|
||||
map_type* theHitsMap = GetMap();
|
||||
for(iterator itr = theHitsMap->begin(); itr != theHitsMap->end(); itr++)
|
||||
delete itr->second;
|
||||
delete theHitsMap;
|
||||
}
|
||||
|
||||
template <typename T> G4int G4THitsMap<T>::operator==(const G4THitsMap<T> &right) const
|
||||
{ return (collectionName==right.collectionName); }
|
||||
//============================================================================//
|
||||
|
||||
template <typename T> inline T*
|
||||
G4THitsMap<T>::operator[](G4int key) const {
|
||||
std::map<G4int,T*> * theHitsMap = GetMap();
|
||||
if(theHitsMap->find(key) != theHitsMap->end()) {
|
||||
return theHitsMap->find(key)->second;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
template <typename T, typename Map_t>
|
||||
G4int G4VTHitsMap<T, Map_t>::operator==(const G4VTHitsMap<T, Map_t> &right) const
|
||||
{
|
||||
return (collectionName==right.collectionName);
|
||||
}
|
||||
|
||||
template <typename T> void G4THitsMap<T>::DrawAllHits()
|
||||
//============================================================================//
|
||||
|
||||
template <typename T, typename Map_t>
|
||||
void G4VTHitsMap<T, Map_t>::DrawAllHits()
|
||||
{;}
|
||||
|
||||
template <typename T> void G4THitsMap<T>::PrintAllHits()
|
||||
//============================================================================//
|
||||
|
||||
template <typename T, typename Map_t>
|
||||
void G4VTHitsMap<T, Map_t>::PrintAllHits()
|
||||
{
|
||||
G4cout << "G4THitsMap " << SDname << " / " << collectionName << " --- " << entries() << " entries" << G4endl;
|
||||
/*----- commented out for the use-case where <T> cannot be initialized
|
||||
to be zero or does not support += operator.
|
||||
std::map<G4int,T*> * theHitsMap = GetMap();
|
||||
typename std::map<G4int, T*>::iterator itr = theHitsMap->begin();
|
||||
T sum = 0.;
|
||||
for(; itr != theHitsMap->end(); itr++) {
|
||||
///////////////////////////////G4cout << " " << itr->first << " : " << *(itr->second) << G4endl;
|
||||
sum += *(itr->second);
|
||||
}
|
||||
G4cout << " Total : " << sum << G4endl;
|
||||
----------------------------------------------------------------------*/
|
||||
G4cout << "G4THitsMap " << SDname << " / " << collectionName << " --- "
|
||||
<< entries() << " entries" << G4endl;
|
||||
/*----- commented out for the use-case where <T> cannot be initialized
|
||||
to be zero or does not support += operator.
|
||||
Map_t * theHitsMap = GetMap();
|
||||
typename Map_t::iterator itr = theHitsMap->begin();
|
||||
T sum = 0.;
|
||||
for(; itr != theHitsMap->end(); itr++) {
|
||||
G4cout << " " << itr->first << " : "
|
||||
<< *(itr->second) << G4endl;
|
||||
sum += *(itr->second);
|
||||
}
|
||||
G4cout << " Total : " << sum << G4endl;
|
||||
----------------------------------------------------------------------*/
|
||||
}
|
||||
|
||||
template <typename T> void G4THitsMap<T>::clear() {
|
||||
//============================================================================//
|
||||
|
||||
std::map<G4int,T*> * theHitsMap = GetMap();
|
||||
typename std::map<G4int, T*>::iterator itr = theHitsMap->begin();
|
||||
for(; itr != theHitsMap->end(); itr++) {
|
||||
delete itr->second;
|
||||
}
|
||||
template <typename T, typename Map_t>
|
||||
void G4VTHitsMap<T, Map_t>::clear()
|
||||
{
|
||||
Map_t * theHitsMap = GetMap();
|
||||
for(iterator itr = theHitsMap->begin(); itr != theHitsMap->end(); itr++)
|
||||
delete itr->second;
|
||||
theHitsMap->clear();
|
||||
|
||||
}
|
||||
|
||||
//============================================================================//
|
||||
// //
|
||||
// //
|
||||
// Helpers for different map types //
|
||||
// //
|
||||
// //
|
||||
//============================================================================//
|
||||
|
||||
template <typename _Tp>
|
||||
class G4THitsMap : public G4VTHitsMap<_Tp, std::map<G4int, _Tp*>>
|
||||
{
|
||||
public:
|
||||
typedef G4VTHitsMap<_Tp, std::map<G4int, _Tp*>> parent_type;
|
||||
|
||||
public:
|
||||
G4THitsMap() : parent_type() { }
|
||||
G4THitsMap(G4String detName,G4String colName)
|
||||
: parent_type(detName, colName) { }
|
||||
|
||||
using parent_type::operator +=;
|
||||
using parent_type::operator ==;
|
||||
using parent_type::operator [];
|
||||
using parent_type::DrawAllHits;
|
||||
using parent_type::PrintAllHits;
|
||||
using parent_type::GetMap;
|
||||
using parent_type::entries;
|
||||
using parent_type::clear;
|
||||
using parent_type::begin;
|
||||
using parent_type::end;
|
||||
using parent_type::cbegin;
|
||||
using parent_type::cend;
|
||||
using parent_type::GetHit;
|
||||
using parent_type::GetSize;
|
||||
using parent_type::add;
|
||||
using parent_type::set;
|
||||
};
|
||||
|
||||
//============================================================================//
|
||||
|
||||
template <typename _Tp>
|
||||
class G4THitsMultiMap : public G4VTHitsMap<_Tp, std::multimap<G4int, _Tp*>>
|
||||
{
|
||||
public:
|
||||
typedef G4VTHitsMap<_Tp, std::multimap<G4int, _Tp*>> parent_type;
|
||||
|
||||
public:
|
||||
G4THitsMultiMap() : parent_type() { }
|
||||
G4THitsMultiMap(G4String detName, G4String colName)
|
||||
: parent_type(detName, colName) { }
|
||||
|
||||
using parent_type::operator +=;
|
||||
using parent_type::operator ==;
|
||||
using parent_type::operator [];
|
||||
using parent_type::DrawAllHits;
|
||||
using parent_type::PrintAllHits;
|
||||
using parent_type::GetMap;
|
||||
using parent_type::entries;
|
||||
using parent_type::clear;
|
||||
using parent_type::begin;
|
||||
using parent_type::end;
|
||||
using parent_type::cbegin;
|
||||
using parent_type::cend;
|
||||
using parent_type::GetHit;
|
||||
using parent_type::GetSize;
|
||||
using parent_type::add;
|
||||
using parent_type::set;
|
||||
};
|
||||
|
||||
//============================================================================//
|
||||
|
||||
template <typename _Tp>
|
||||
class G4THitsUnorderedMap
|
||||
: public G4VTHitsMap<_Tp, std::unordered_map<G4int, _Tp*>>
|
||||
{
|
||||
public:
|
||||
typedef G4VTHitsMap<_Tp, std::unordered_map<G4int, _Tp*>> parent_type;
|
||||
|
||||
public:
|
||||
G4THitsUnorderedMap() : parent_type() { }
|
||||
G4THitsUnorderedMap(G4String detName, G4String colName)
|
||||
: parent_type(detName, colName) { }
|
||||
|
||||
using parent_type::operator +=;
|
||||
using parent_type::operator ==;
|
||||
using parent_type::operator [];
|
||||
using parent_type::DrawAllHits;
|
||||
using parent_type::PrintAllHits;
|
||||
using parent_type::GetMap;
|
||||
using parent_type::entries;
|
||||
using parent_type::clear;
|
||||
using parent_type::begin;
|
||||
using parent_type::end;
|
||||
using parent_type::cbegin;
|
||||
using parent_type::cend;
|
||||
using parent_type::GetHit;
|
||||
using parent_type::GetSize;
|
||||
using parent_type::add;
|
||||
using parent_type::set;
|
||||
};
|
||||
|
||||
//============================================================================//
|
||||
|
||||
template <typename _Tp>
|
||||
class G4THitsUnorderedMultiMap
|
||||
: public G4VTHitsMap<_Tp, std::unordered_multimap<G4int, _Tp*>>
|
||||
{
|
||||
public:
|
||||
typedef G4VTHitsMap<_Tp, std::unordered_multimap<G4int, _Tp*>> parent_type;
|
||||
|
||||
public:
|
||||
G4THitsUnorderedMultiMap() : parent_type() { }
|
||||
G4THitsUnorderedMultiMap(G4String detName,G4String colName)
|
||||
: parent_type(detName, colName) { }
|
||||
|
||||
using parent_type::operator +=;
|
||||
using parent_type::operator ==;
|
||||
using parent_type::operator [];
|
||||
using parent_type::DrawAllHits;
|
||||
using parent_type::PrintAllHits;
|
||||
using parent_type::GetMap;
|
||||
using parent_type::entries;
|
||||
using parent_type::clear;
|
||||
using parent_type::begin;
|
||||
using parent_type::end;
|
||||
using parent_type::cbegin;
|
||||
using parent_type::cend;
|
||||
using parent_type::GetHit;
|
||||
using parent_type::GetSize;
|
||||
using parent_type::add;
|
||||
using parent_type::set;
|
||||
};
|
||||
|
||||
//============================================================================//
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user