Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4AccumulableManager.cc
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// Author: Ivana Hrivnacova, 18/06/2013 ([email protected])
28
31#include "G4Threading.hh"
32#include "G4AutoLock.hh"
33
34// mutex in a file scope
35
36namespace {
37 //Mutex to lock master manager when merging accumulables
38 G4Mutex mergeMutex = G4MUTEX_INITIALIZER;
39}
40
41//_____________________________________________________________________________
47
48//_____________________________________________________________________________
49G4AccumulableManager::G4AccumulableManager()
50{
51 if ( ! G4Threading::IsWorkerThread() ) fgMasterInstance = this;
52}
53
54//_____________________________________________________________________________
56{
57 // delete only accumulables create by the mager itself
58 for ( auto it : fAccumulablesToDelete ) {
59 delete it;
60 }
61}
62
63//
64// private methods
65//
66
67//_____________________________________________________________________________
68G4String G4AccumulableManager::GenerateName() const
69{
70 G4String name = kBaseName;
71 std::ostringstream os;
72 os << fVector.size();
73 name.append("_");
74 name.append(os.str());
75 return name;
76}
77
78//_____________________________________________________________________________
79G4bool G4AccumulableManager::CheckName(const G4String& name, const G4String& where) const
80{
81 if ( fMap.find(name) == fMap.end() ) return true;
82
83 G4ExceptionDescription description;
84 description << "Name " << name << " is already used." << G4endl;
85 description << "Parameter will be not created/registered.";
86 G4String method("G4AccumulableManager::");
87 method.append(where);
88 G4Exception(method, "Analysis_W001", JustWarning, description);
89 return false;
90}
91
92//
93// public methods
94//
95
96//_____________________________________________________________________________
98{
99 auto name = accumulable->GetName();
100
101 // do not accept name if it is already used
102 if ( ! CheckName(name, "RegisterAccumulable") ) return false;
103
104 // generate name if empty
105 if (name.length() == 0u) {
106 name = GenerateName();
107 accumulable->fName = name;
108 }
109
110 fMap[name] = accumulable;
111 fVector.push_back(accumulable);
112 return true;
113}
114
115//_____________________________________________________________________________
118{
119 // get G4VParammeter from the map
120 auto it = fMap.find(name);
121 if ( it == fMap.end() ) {
122 if ( warn) {
123 G4ExceptionDescription description;
124 description << "Accumulable " << name << " does not exist.";
125 G4Exception("G4AccumulableManager::GetAccumulable",
126 "Analysis_W001", JustWarning, description);
127 }
128 return nullptr;
129 }
130
131 return it->second;
132}
133
134//_____________________________________________________________________________
137{
138 // get G4VParammeter from the vector
139 if ( id < 0 || id >= G4int(fVector.size()) ) {
140 if ( warn) {
141 G4ExceptionDescription description;
142 description << "Accumulable " << id << " does not exist.";
143 G4Exception("G4AccumulableManager::GetAccumulable",
144 "Analysis_W001", JustWarning, description);
145 }
146 return nullptr;
147 }
148
149 return fVector[id];
150}
151
152//_____________________________________________________________________________
154{
155 // Do nothing if there are no accumulables registered
156 // or if master thread
157 if ((fVector.size() == 0u) || (! G4Threading::IsWorkerThread())) return;
158
159 // The manager on mastter must exist
160 if (fgMasterInstance == nullptr) {
161 G4ExceptionDescription description;
162 description
163 << "No master G4AccumulableManager instance exists." << G4endl
164 << "Accumulables will not be merged.";
165 G4Exception("G4AccumulableManager::Merge()",
166 "Analysis_W001", JustWarning, description);
167 return;
168 }
169
170 // The worker manager just merges its accumulables to the master
171 // This operation needs a lock
172 // G4cout << "Go to merge accumulables" << G4endl;
173 G4AutoLock lock(&mergeMutex);
174
175 // the other manager has the vector with the "same" accumulables
176 auto it = fVector.begin();
177 for ( auto itMaster : fgMasterInstance->fVector ) {
178 // G4VAccumulable* masterAccumulable = itMaster;
179 // G4VAccumulable* accumulable = *(it++);
180 // masterAccumulable->Merge(*(accumulable));
181 itMaster->Merge(*(*(it++)));
182 }
183 lock.unlock();
184}
185
186//_____________________________________________________________________________
188{
189// Reset histograms and profiles
190
191 for ( auto it : fVector ) {
192 it->Reset();
193 }
194}
195
196
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
#define G4MUTEX_INITIALIZER
std::mutex G4Mutex
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
#define G4endl
Definition G4ios.hh:67
G4Accumulable< T > * GetAccumulable(const G4String &name, G4bool warn=true) const
static G4AccumulableManager * Instance()
G4bool RegisterAccumulable(G4Accumulable< T > &accumulable)
G4String GetName() const
const char * name(G4int ptype)
G4bool IsWorkerThread()