Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4THitsCollection.hh
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//
27//
28
29#ifndef G4THitsCollection_h
30#define G4THitsCollection_h 1
31
32#include "G4Allocator.hh"
33#include "G4VHitsCollection.hh"
34#include "globals.hh"
35
36#include <vector>
37
38// class description:
39//
40// This is a template class of hits collection and parametrized by
41// The concrete class of G4VHit. This is a uniform collection for
42// a particular concrete hit class objects.
43// An intermediate layer class G4HitsCollection appeared in this
44// header file is used just for G4Allocator, because G4Allocator
45// cannot be instansiated with a template class. Thus G4HitsCollection
46// class MUST NOT be directly used by the user.
47
49{
50 public:
52 ~G4HitsCollection() override = default;
53 G4bool operator==(const G4HitsCollection& right) const;
54
55 protected:
56 void* theCollection = nullptr;
57};
58
59#if defined G4DIGI_ALLOC_EXPORT
61#else
63#endif
64
65template <class T>
67{
68 public:
70 G4THitsCollection(G4String detName, G4String colNam);
71 ~G4THitsCollection() override;
72
73 G4bool operator==(const G4THitsCollection<T>& right) const;
74
75 inline void* operator new(size_t);
76 inline void operator delete(void* anHC);
77
78 // Invoke Draw() method on all hit objects in collection
79 void DrawAllHits() override;
80
81 // Invoke Print() method on all hit objects in collection
82 void PrintAllHits() override;
83
84 // Returns a pointer to the concrete hit object
85 // Returns pointer to the concrete hit object at given index
86 // Not bounds checked
87 inline T* operator[](size_t i) const { return (*((std::vector<T*>*)theCollection))[i]; }
88
89 // Return pointer to hit collection
90 inline std::vector<T*>* GetVector() const { return (std::vector<T*>*)theCollection; }
91
92 // Insert a hit object in the collection, taking onwenership
93 // Returns the total number of hit objects stored after insertion
94 inline size_t insert(T* aHit)
95 {
96 auto theHitsCollection = (std::vector<T*>*)theCollection;
97 theHitsCollection->push_back(aHit);
98 return theHitsCollection->size();
99 }
100
101 // Returns the number of hit objects stored in this collection.
102 inline size_t entries() const
103 {
104 auto theHitsCollection = (std::vector<T*>*)theCollection;
105 return theHitsCollection->size();
106 }
107
108 G4VHit* GetHit(size_t i) const override { return (*((std::vector<T*>*)theCollection))[i]; }
109
110 size_t GetSize() const override { return ((std::vector<T*>*)theCollection)->size(); }
111};
112
113template <class T>
114inline void* G4THitsCollection<T>::operator new(size_t)
115{
116 if (anHCAllocator_G4MT_TLS_() == nullptr) {
118 }
119 return (void*)anHCAllocator_G4MT_TLS_()->MallocSingle();
120}
121
122template <class T>
123inline void G4THitsCollection<T>::operator delete(void* anHC)
124{
125 anHCAllocator_G4MT_TLS_()->FreeSingle((G4HitsCollection*)anHC);
126}
127
128template <class T>
130{
131 auto theHitsCollection = new std::vector<T*>;
132 theCollection = (void*)theHitsCollection;
133}
134
135template <class T>
137 : G4HitsCollection(detName, colNam)
138{
139 auto theHitsCollection = new std::vector<T*>;
140 theCollection = (void*)theHitsCollection;
141}
142
143template <class T>
145{
146 auto theHitsCollection = (std::vector<T*>*)theCollection;
147 for (const auto* hit : *theHitsCollection) {
148 delete hit;
149 }
150 theHitsCollection->clear();
151 delete theHitsCollection;
152}
153
154template <class T>
156{
157 return (collectionName == right.collectionName);
158}
159
160template <class T>
162{
163 auto theHitsCollection = (std::vector<T*>*)theCollection;
164 for (auto* hit : *theHitsCollection) {
165 hit->Draw();
166 }
167}
168
169template <class T>
171{
172 auto theHitsCollection = (std::vector<T*>*)theCollection;
173 for (auto* hit : *theHitsCollection) {
174 hit->Print();
175 }
176}
177
178#endif
G4DLLIMPORT G4Allocator< G4HitsCollection > *& anHCAllocator_G4MT_TLS_()
#define G4DLLIMPORT
Definition G4Types.hh:69
#define G4DLLEXPORT
Definition G4Types.hh:68
bool G4bool
Definition G4Types.hh:86
G4bool operator==(const G4HitsCollection &right) const
~G4HitsCollection() override=default
G4VHit * GetHit(size_t i) const override
size_t GetSize() const override
std::vector< T * > * GetVector() const
void PrintAllHits() override
G4bool operator==(const G4THitsCollection< T > &right) const
size_t insert(T *aHit)
T * operator[](size_t i) const
void DrawAllHits() override
G4VHitsCollection()=default