Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 // $Id: G4AccumulableManager.cc 91116 2015-06-20 12:33:45Z ihrivnac $
27 
28 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
29 
30 #include "G4AccumulableManager.hh"
31 #include "G4Threading.hh"
32 #include "G4AutoLock.hh"
33 
34 // mutex in a file scope
35 
36 namespace {
37  //Mutex to lock master manager when merging accumulables
38  G4Mutex mergeMutex = G4MUTEX_INITIALIZER;
39 }
40 
41 G4AccumulableManager* G4AccumulableManager::fgMasterInstance = nullptr;
42 G4ThreadLocal G4AccumulableManager* G4AccumulableManager::fgInstance = nullptr;
43 
44 //_____________________________________________________________________________
46 {
47  if ( fgInstance == nullptr ) {
48  G4bool isMaster = ! G4Threading::IsWorkerThread();
49  fgInstance = new G4AccumulableManager(isMaster);
50  }
51 
52  return fgInstance;
53 }
54 
55 //_____________________________________________________________________________
56 G4AccumulableManager::G4AccumulableManager(G4bool isMaster)
57  : fVector(),
58  fMap()
59 {
60  if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
61  G4ExceptionDescription description;
62  description
63  << " "
64  << "G4AccumulableAnalysisManager already exists."
65  << "Cannot create another instance.";
66  G4Exception("G4AccumulableAnalysisManager::G4AccumulableAnalysisManager()",
67  "Analysis_F001", FatalException, description);
68  }
69  if ( isMaster ) fgMasterInstance = this;
70  fgInstance = this;
71 }
72 
73 //_____________________________________________________________________________
75 {
76  // delete only accumulables create by the mager itself
77  for ( auto it : fAccumulablesToDelete ) {
78  delete it;
79  }
80 }
81 
82 //
83 // private methods
84 //
85 
86 //_____________________________________________________________________________
87 G4String G4AccumulableManager::GenerateName() const
88 {
89  G4String name = kBaseName;
90  std::ostringstream os;
91  os << fVector.size();
92  name.append("_");
93  name.append(os.str());
94  return name;
95 }
96 
97 //_____________________________________________________________________________
98 G4bool G4AccumulableManager::CheckName(const G4String& name, const G4String& where) const
99 {
100  if ( fMap.find(name) == fMap.end() ) return true;
101 
102  G4ExceptionDescription description;
103  description << " " << "Name " << name << " is already used." << G4endl;
104  description << " " << "Paremeter will be not created/registered.";
105  G4String method("G4AccumulableManager::");
106  method.append(where);
107  G4Exception(method, "Analysis_W002", JustWarning, description);
108  return false;
109 }
110 
111 //
112 // public methods
113 //
114 
115 //_____________________________________________________________________________
117 {
118  auto name = accumulable->GetName();
119 
120  // do not accept name if it is already used
121  if ( ! CheckName(name, "RegisterAccumulable") ) return false;
122 
123  // generate name if empty
124  if ( ! name.length() ) {
125  name = GenerateName();
126  accumulable->fName = name;
127  }
128 
129  fMap[name] = accumulable;
130  fVector.push_back(accumulable);
131  return true;
132 }
133 
134 //_____________________________________________________________________________
137 {
138  // get G4VParammeter from the map
139  auto it = fMap.find(name);
140  if ( it == fMap.end() ) {
141  if ( warn) {
142  G4ExceptionDescription description;
143  description << " " << "accumulable " << name << " does not exist.";
144  G4Exception("G4AccumulableManager::GetAccumulable",
145  "Analysis_W011", JustWarning, description);
146  }
147  return nullptr;
148  }
149 
150  return it->second;
151 }
152 
153 //_____________________________________________________________________________
156 {
157  // get G4VParammeter from the vector
158  if ( id < 0 || id >= G4int(fVector.size()) ) {
159  if ( warn) {
160  G4ExceptionDescription description;
161  description << " " << "accumulable " << id << " does not exist.";
162  G4Exception("G4AccumulableManager::GetAccumulable",
163  "Analysis_W011", JustWarning, description);
164  }
165  return nullptr;
166  }
167 
168  return fVector[id];
169 }
170 
171 //_____________________________________________________________________________
173 {
174  // Do nothing if there are no accumulables registered
175  // or if master thread
176  if ( (! fVector.size()) || (! G4Threading::IsWorkerThread()) ) return;
177 
178  // The manager on mastter must exist
179  if ( ! fgMasterInstance ) {
180  G4ExceptionDescription description;
181  description
182  << " " << "No master G4AccumulableManager instance exists."
183  << G4endl
184  << " " << "Accumulables will not be merged.";
185  G4Exception("G4AccumulableManager::Merge()",
186  "Analysis_W031", JustWarning, description);
187  return;
188  }
189 
190  // The worker manager just merges its accumulables to the master
191  // This operation needs a lock
192  // G4cout << "Go to merge accumulables" << G4endl;
193  G4AutoLock lock(&mergeMutex);
194 
195  // the other manager has the vector with the "same" accumulables
196  auto it = fVector.begin();
197  for ( auto itMaster : fgMasterInstance->fVector ) {
198  // G4VAccumulable* masterAccumulable = itMaster;
199  // G4VAccumulable* accumulable = *(it++);
200  // masterAccumulable->Merge(*(accumulable));
201  itMaster->Merge(*(*(it++)));
202  }
203  lock.unlock();
204 }
205 
206 //_____________________________________________________________________________
208 {
209 // Reset histograms and profiles
210 
211  for ( auto it : fVector ) {
212  it->Reset();
213  }
214 }
215 
216 
const XML_Char * name
Definition: expat.h:151
G4Accumulable< T > * GetAccumulable(const G4String &name, G4bool warn=true) const
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4String GetName() const
#define G4ThreadLocal
Definition: tls.hh:89
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
static G4AccumulableManager * Instance()
G4bool RegisterAccumulable(G4Accumulable< T > &accumulable)
bool G4bool
Definition: G4Types.hh:79
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool IsWorkerThread()
Definition: G4Threading.cc:145
G4int G4Mutex
Definition: G4Threading.hh:173
G4String & append(const G4String &)
#define G4endl
Definition: G4ios.hh:61