Geant4  10.02.p02
G4ParameterManager.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: G4ParameterManager.cc 91116 2015-06-20 12:33:45Z ihrivnac $
27 
28 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
29 
30 #include "G4ParameterManager.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 parameters
38  G4Mutex mergeMutex = G4MUTEX_INITIALIZER;
39 }
40 
43 
44 //_____________________________________________________________________________
46 {
47  if ( fgInstance == nullptr ) {
48  G4bool isMaster = ! G4Threading::IsWorkerThread();
49  fgInstance = new G4ParameterManager(isMaster);
50  }
51 
52  return fgInstance;
53 }
54 
55 //_____________________________________________________________________________
57  : fState("Parameter", isMaster),
58  fMap()
59 {
60  if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
61  G4ExceptionDescription description;
62  description
63  << " "
64  << "G4ParameterAnalysisManager already exists."
65  << "Cannot create another instance.";
66  G4Exception("G4ParameterAnalysisManager::G4ParameterAnalysisManager()",
67  "Analysis_F001", FatalException, description);
68  }
69  if ( isMaster ) fgMasterInstance = this;
70  fgInstance = this;
71 }
72 
73 //_____________________________________________________________________________
75 {
76  // delete only parameters create by the mager itself
77  for ( auto it : fParametersToDelete ) {
78  delete it;
79  }
80 }
81 
82 //
83 // public methods
84 //
85 
86 //_____________________________________________________________________________
88 {
89  fMap[parameter->GetName()] = parameter;
90 }
91 
92 //_____________________________________________________________________________
95 {
96  // get G4VParammeter from the map
97  auto it = fMap.find(name);
98  if ( it == fMap.end() ) {
99  if ( warn) {
100  G4ExceptionDescription description;
101  description << " " << "parameter " << name << " does not exist.";
102  G4Exception("G4ParameterManager::GetParameter",
103  "Analysis_W011", JustWarning, description);
104  }
105  return 0;
106  }
107 
108  return it->second;
109 }
110 
111 // //_____________________________________________________________________________
112 // G4bool G4ParameterManager::MergeImpl(tools::histo::hmpi* hmpi)
113 // {
114 // // tbd
115 // return false;
116 // }
117 
118 //_____________________________________________________________________________
120 {
121  // Do nothing if there are no parameters registered
122  // or if master thread
123  if ( (! fMap.size()) || (! G4Threading::IsWorkerThread()) ) return;
124 
125  // The manager on mastter must exist
126  if ( ! fgMasterInstance ) {
127  G4ExceptionDescription description;
128  description
129  << " " << "No master G4ParameterManager instance exists."
130  << G4endl
131  << " " << "Parameters will not be merged.";
132  G4Exception("G4ParameterManager::Merge()",
133  "Analysis_W031", JustWarning, description);
134  return;
135  }
136 
137  // The worker manager just merges its parameters to the master
138  // This operation needs a lock
139  G4cout << "Go to merge parameters" << G4endl;
140  G4AutoLock lock(&mergeMutex);
141 
142  // the other manager has the map with identical keys
143  auto it = fMap.begin();
144  for ( auto itMaster : fgMasterInstance->fMap ) {
145  // G4VParameter* parameter = (it++)->second;
146  // G4VParameter* masterParameter = itMaster->second;
147  // masterParameter->Merge(*(parameter));
148  itMaster.second->Merge(*((it++)->second));
149  }
150  lock.unlock();
151 }
152 
153 //_____________________________________________________________________________
155 {
156 // Reset histograms and profiles
157 
158  for ( auto it : fMap ) {
159  it.second->Reset();
160  }
161 }
162 
163 
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4String name
Definition: TRTMaterials.hh:40
static G4ParameterManager * fgMasterInstance
static G4ParameterManager * Instance()
#define G4ThreadLocal
Definition: tls.hh:89
std::vector< G4VParameter * > fParametersToDelete
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
G4String GetName() const
G4GLOB_DLL std::ostream G4cout
bool G4bool
Definition: G4Types.hh:79
std::map< G4String, G4VParameter * > fMap
static G4ThreadLocal G4ParameterManager * fgInstance
static const double second
Definition: G4SIunits.hh:156
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool IsWorkerThread()
Definition: G4Threading.cc:129
G4int G4Mutex
Definition: G4Threading.hh:173
#define G4endl
Definition: G4ios.hh:61
G4Parameter< T > * GetParameter(const G4String &name, G4bool warn=true) const
G4ParameterManager(G4bool isMaster=true)
void RegisterParameter(G4Parameter< T > &parameter)