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 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /analysis/accumulables/src/G4AccumulableManager.cc (Version 11.3.0) and /analysis/accumulables/src/G4AccumulableManager.cc (Version 10.5)


  1 //                                                  1 //
  2 // *******************************************      2 // ********************************************************************
  3 // * License and Disclaimer                         3 // * License and Disclaimer                                           *
  4 // *                                                4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of th      5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided      6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License      7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/      8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.           9 // * include a list of copyright holders.                             *
 10 // *                                               10 // *                                                                  *
 11 // * Neither the authors of this software syst     11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing fin     12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warran     13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assum     14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file      15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitatio     16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                               17 // *                                                                  *
 18 // * This  code  implementation is the result      18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboratio     19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distri     20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  ag     21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publicati     22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Sof     23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // *******************************************     24 // ********************************************************************
 25 //                                                 25 //
 26                                                    26 
 27 // Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS <<  27 // Author: Ivana Hrivnacova, 18/06/2013  (ivana@ipno.in2p3.fr)
 28                                                    28 
 29 #include "G4AccumulableManager.hh"                 29 #include "G4AccumulableManager.hh"
 30 #include "G4ThreadLocalSingleton.hh"           << 
 31 #include "G4Threading.hh"                          30 #include "G4Threading.hh"
 32 #include "G4AutoLock.hh"                           31 #include "G4AutoLock.hh"
 33                                                    32 
 34 // mutex in a file scope                           33 // mutex in a file scope
 35                                                    34 
 36 namespace {                                        35 namespace {
 37   //Mutex to lock master manager when merging      36   //Mutex to lock master manager when merging accumulables
 38   G4Mutex mergeMutex = G4MUTEX_INITIALIZER;        37   G4Mutex mergeMutex = G4MUTEX_INITIALIZER;
 39                                                << 
 40   void RangeException(const G4String& where, c << 
 41   {                                            << 
 42     G4ExceptionDescription description;        << 
 43     description << "Invalid range " << message << 
 44     G4String method("G4AccumulableManager::"); << 
 45     method.append(where);                      << 
 46     G4Exception(where, "Analysis_W001", JustWa << 
 47   }                                            << 
 48 }                                                  38 }
 49                                                    39 
                                                   >>  40 G4AccumulableManager* G4AccumulableManager::fgMasterInstance = nullptr;
                                                   >>  41 G4ThreadLocal G4AccumulableManager* G4AccumulableManager::fgInstance = nullptr;
                                                   >>  42 
 50 //____________________________________________     43 //_____________________________________________________________________________
 51 G4AccumulableManager* G4AccumulableManager::In     44 G4AccumulableManager* G4AccumulableManager::Instance()
 52 {                                                  45 {
 53   static G4ThreadLocalSingleton<G4AccumulableM <<  46   if ( fgInstance == nullptr ) {
 54   return instance.Instance();                  <<  47     G4bool isMaster = ! G4Threading::IsWorkerThread();
 55 }                                              <<  48     fgInstance = new G4AccumulableManager(isMaster);
                                                   >>  49   }
                                                   >>  50   
                                                   >>  51   return fgInstance;
                                                   >>  52 }    
 56                                                    53 
 57 //____________________________________________     54 //_____________________________________________________________________________
 58 G4AccumulableManager::G4AccumulableManager()   <<  55 G4AccumulableManager::G4AccumulableManager(G4bool isMaster)
                                                   >>  56  : fVector(),
                                                   >>  57    fMap() 
 59 {                                                  58 {
 60   if (!G4Threading::IsWorkerThread()) {        <<  59   if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
 61     fgMasterInstance = this;                   <<  60     G4ExceptionDescription description;
                                                   >>  61     description 
                                                   >>  62       << "      " 
                                                   >>  63       << "G4AccumulableAnalysisManager already exists." 
                                                   >>  64       << "Cannot create another instance.";
                                                   >>  65     G4Exception("G4AccumulableAnalysisManager::G4AccumulableAnalysisManager()",
                                                   >>  66                 "Analysis_F001", FatalException, description);
 62   }                                                67   }
                                                   >>  68   if ( isMaster ) fgMasterInstance = this;
                                                   >>  69   fgInstance = this;
 63 }                                                  70 }
 64                                                    71 
 65 //____________________________________________     72 //_____________________________________________________________________________
 66 G4AccumulableManager::~G4AccumulableManager()      73 G4AccumulableManager::~G4AccumulableManager()
 67 {                                                  74 {
 68   // delete only accumulables create by the ma     75   // delete only accumulables create by the mager itself
 69   for ( auto it : fAccumulablesToDelete ) {        76   for ( auto it : fAccumulablesToDelete ) {
 70     delete it;                                     77     delete it;
 71   }                                                78   }
 72 }                                                  79 }
 73                                                    80 
 74 //                                                 81 //
 75 // private methods                                 82 // private methods
 76 //                                                 83 //
 77                                                    84 
 78 //____________________________________________     85 //_____________________________________________________________________________
 79 G4String G4AccumulableManager::GenerateName()      86 G4String G4AccumulableManager::GenerateName() const
 80 {                                                  87 {
 81   G4String name = kBaseName;                       88   G4String name = kBaseName;
 82   std::ostringstream os;                           89   std::ostringstream os;
 83   os << fVector.size();                            90   os << fVector.size();
 84   name.append("_");                                91   name.append("_");
 85   name.append(os.str());                           92   name.append(os.str());
 86   return name;                                     93   return name;
 87 }                                                  94 }
 88                                                    95 
 89 //____________________________________________     96 //_____________________________________________________________________________
 90 G4bool G4AccumulableManager::CheckName(        <<  97 G4bool G4AccumulableManager::CheckName(const G4String& name, const G4String& where) const
 91   const G4String& name, const G4String& where) << 
 92 {                                                  98 {
 93   if (fMap.find(name) == fMap.end()) {         <<  99   if ( fMap.find(name) == fMap.end() ) return true;
 94     return true;                               << 100  
 95   }                                            << 
 96                                                << 
 97   G4ExceptionDescription description;             101   G4ExceptionDescription description;
 98   description << "Name " << name << " is alrea << 102   description << "      " << "Name " << name << " is already used." << G4endl;
 99   description << "Parameter will be not create << 103   description << "      " << "Paremeter will be not created/registered.";
100   G4String method("G4AccumulableManager::");      104   G4String method("G4AccumulableManager::");
101   method.append(where);                           105   method.append(where);
102   G4Exception(method, "Analysis_W001", JustWar << 106   G4Exception(method, "Analysis_W002", JustWarning, description);
103   return false;                                   107   return false;
104 }                                                 108 }
105                                                   109 
106 //____________________________________________ << 
107 G4bool G4AccumulableManager::CheckType(        << 
108   G4VAccumulable* accumulable, G4AccType type, << 
109 {                                              << 
110   if (accumulable->GetType() != type) {        << 
111     if (warn) {                                << 
112       G4ExceptionDescription description;      << 
113       description << "      " << accumulable-> << 
114       G4Exception("G4AccumulableManager::Check << 
115                   "Analysis_W001", JustWarning << 
116     }                                          << 
117     return false;                              << 
118   }                                            << 
119   return true;                                 << 
120 }                                              << 
121                                                << 
122 //                                                110 //
123 // public methods                                 111 // public methods
124 //                                                112 //
125                                                   113 
126 //____________________________________________    114 //_____________________________________________________________________________
127 G4bool G4AccumulableManager::Register(G4VAccum << 115 G4bool G4AccumulableManager::RegisterAccumulable(G4VAccumulable* accumulable)
128 {                                                 116 {
129   auto name = accumulable->GetName();             117   auto name = accumulable->GetName();
130                                                << 118   
131   if (G4Accumulables::VerboseLevel > 1 ) {     << 
132     G4cout << "Going to register accumulable \ << 
133   }                                            << 
134                                                << 
135   // do not accept name if it is already used     119   // do not accept name if it is already used
136   if (!CheckName(name, "RegisterAccumulable")) << 120   if ( ! CheckName(name, "RegisterAccumulable") ) return false; 
137     return false;                              << 
138   }                                            << 
139                                                   121 
140   // generate name if empty                       122   // generate name if empty
141   if (name.length() == 0u) {                   << 123   if ( ! name.length() ) {
142     name =  GenerateName();                       124     name =  GenerateName();
143     accumulable->SetName(name);                << 125     accumulable->fName = name;
144   }                                               126   }
145                                                   127 
146   // set Id                                    << 
147   accumulable->SetId((G4int)fVector.size());   << 
148                                                << 
149   fMap[name] = accumulable;                       128   fMap[name] = accumulable;
150   fVector.push_back(accumulable);                 129   fVector.push_back(accumulable);
151                                                << 
152   if (G4Accumulables::VerboseLevel > 0 ) {     << 
153     G4cout << "Accumulable registered as \"" < << 
154   }                                            << 
155                                                << 
156   return true;                                    130   return true;
157 }                                                 131 }
158                                                   132 
159 // Deprecated functions with long name         << 
160 //____________________________________________ << 
161 G4bool G4AccumulableManager::RegisterAccumulab << 
162 {                                              << 
163   return Register(accumulable);                << 
164 }                                              << 
165                                                << 
166 //____________________________________________    133 //_____________________________________________________________________________
167 G4VAccumulable*                                << 134 G4VAccumulable*  
168 G4AccumulableManager::GetAccumulable(const G4S    135 G4AccumulableManager::GetAccumulable(const G4String& name, G4bool warn) const
169 {                                                 136 {
170   // get G4VParammeter from the map               137   // get G4VParammeter from the map
171   auto it = fMap.find(name);                      138   auto it = fMap.find(name);
172   if ( it == fMap.end() ) {                       139   if ( it == fMap.end() ) {
173     if ( warn) {                                  140     if ( warn) {
174       G4ExceptionDescription description;         141       G4ExceptionDescription description;
175       description << "Accumulable " << name << << 142       description << "      " << "accumulable " << name << " does not exist.";
176       G4Exception("G4AccumulableManager::GetAc << 143       G4Exception("G4AccumulableManager::GetAccumulable", 
177                   "Analysis_W001", JustWarning << 144                   "Analysis_W011", JustWarning, description);
178     }                                             145     }
179     return nullptr;                               146     return nullptr;
180   }                                               147   }
181                                                   148 
182   return it->second;                              149   return it->second;
183 }                                                 150 }
184                                                   151 
185 //____________________________________________    152 //_____________________________________________________________________________
186 G4VAccumulable*                                << 153 G4VAccumulable*  
187 G4AccumulableManager::GetAccumulable(G4int id,    154 G4AccumulableManager::GetAccumulable(G4int id, G4bool warn) const
188 {                                                 155 {
189   // get G4VParammeter from the vector            156   // get G4VParammeter from the vector
190   if ( id < 0 || id >= G4int(fVector.size()) )    157   if ( id < 0 || id >= G4int(fVector.size()) ) {
191     if ( warn) {                                  158     if ( warn) {
192       G4ExceptionDescription description;         159       G4ExceptionDescription description;
193       description << "Accumulable " << id << " << 160       description << "      " << "accumulable " << id << " does not exist.";
194       G4Exception("G4AccumulableManager::GetAc << 161       G4Exception("G4AccumulableManager::GetAccumulable", 
195                   "Analysis_W001", JustWarning << 162                   "Analysis_W011", JustWarning, description);
196     }                                             163     }
197     return nullptr;                               164     return nullptr;
198   }                                               165   }
199                                                   166 
200   return fVector[id];                             167   return fVector[id];
201 }                                                 168 }
202                                                   169 
203 //____________________________________________    170 //_____________________________________________________________________________
204 void G4AccumulableManager::Merge()             << 171 void G4AccumulableManager::Merge() 
205 {                                                 172 {
206   // Do nothing if  there are no accumulables     173   // Do nothing if  there are no accumulables registered
207   // or if master thread                          174   // or if master thread
208   if ((fVector.size() == 0u) || (!G4Threading: << 175   if ( (! fVector.size()) ||  (! G4Threading::IsWorkerThread()) ) return;
209     return;                                    << 
210   }                                            << 
211                                                   176 
212   // The manager on mastter must exist            177   // The manager on mastter must exist
213   if (fgMasterInstance == nullptr) {           << 178   if ( ! fgMasterInstance ) {
214     G4ExceptionDescription description;           179     G4ExceptionDescription description;
215     description                                << 180     description 
216       << "No master G4AccumulableManager insta << 181       << "      " << "No master G4AccumulableManager instance exists." 
217       << "Accumulables will not be merged.";   << 182       << G4endl 
                                                   >> 183       << "      " << "Accumulables will not be merged.";
218       G4Exception("G4AccumulableManager::Merge    184       G4Exception("G4AccumulableManager::Merge()",
219                 "Analysis_W001", JustWarning,  << 185                 "Analysis_W031", JustWarning, description);
220     return;                                       186     return;
221   }                                               187   }
222                                                   188 
223   // The worker manager just merges its accumu    189   // The worker manager just merges its accumulables to the master
224   // This operation needs a lock                  190   // This operation needs a lock
225   // G4cout << "Go to merge accumulables" << G << 191   // G4cout << "Go to merge accumulables" << G4endl; 
226   G4AutoLock lock(&mergeMutex);                   192   G4AutoLock lock(&mergeMutex);
227                                                   193 
228   // the other manager has the vector with the    194   // the other manager has the vector with the "same" accumulables
229   auto it = fVector.begin();                      195   auto it = fVector.begin();
230   for ( auto itMaster : fgMasterInstance->fVec    196   for ( auto itMaster : fgMasterInstance->fVector ) {
231     // G4VAccumulable* masterAccumulable = itM    197     // G4VAccumulable* masterAccumulable = itMaster;
232     // G4VAccumulable* accumulable = *(it++);     198     // G4VAccumulable* accumulable = *(it++);
233     // masterAccumulable->Merge(*(accumulable)    199     // masterAccumulable->Merge(*(accumulable));
234     itMaster->Merge(*(*(it++)));                  200     itMaster->Merge(*(*(it++)));
235   }                                            << 201   }  
236   lock.unlock();                                  202   lock.unlock();
237 }                                                 203 }
238                                                   204 
239 //____________________________________________    205 //_____________________________________________________________________________
240 void G4AccumulableManager::Reset()                206 void G4AccumulableManager::Reset()
241 {                                                 207 {
242 // Reset all accummulables                     << 208 // Reset histograms and profiles
243                                                << 209   
244   for ( auto it : fVector ) {                     210   for ( auto it : fVector ) {
245     it->Reset();                                  211     it->Reset();
246   }                                            << 212   }  
247 }                                              << 213 }  
248                                                << 214  
249 //____________________________________________ << 
250 void G4AccumulableManager::Print(G4PrintOption << 
251 {                                              << 
252   for ( auto it : fVector ) {                  << 
253     it->Print(options);                        << 
254   }                                            << 
255 }                                              << 
256                                                   215 
257 //____________________________________________ << 
258 void G4AccumulableManager::Print(              << 
259   G4int startId, G4int count, G4PrintOptions o << 
260 {                                              << 
261   // check if range is within limits           << 
262   if ( startId < 0 || startId >= G4int(fVector << 
263        count <= 0 || startId + count > G4int(f << 
264     RangeException("Print",                    << 
265       std::to_string(startId) + ", " + std::to << 
266     return;                                    << 
267   }                                            << 
268                                                << 
269   for ( auto id = startId; id < startId + coun << 
270     fVector[id]->Print(options);               << 
271   }                                            << 
272 }                                              << 
273                                                << 
274 //____________________________________________ << 
275 void G4AccumulableManager::Print(              << 
276   std::vector<G4VAccumulable*>::iterator start << 
277   std::vector<G4VAccumulable*>::iterator endIt << 
278   G4PrintOptions options) const                << 
279 {                                              << 
280   // check if range is within limits           << 
281   if ( startIt == fVector.end() || endIt == fV << 
282     RangeException("Print", "[startIt, endIt]" << 
283     return;                                    << 
284   }                                            << 
285                                                << 
286   for ( auto it = startIt; it != endIt; ++it ) << 
287     (*it)->Print(options);                     << 
288   }                                            << 
289 }                                              << 
290                                                << 
291 //____________________________________________ << 
292 void G4AccumulableManager::Print(              << 
293   std::vector<G4VAccumulable*>::iterator start << 
294   G4PrintOptions options) const                << 
295 {                                              << 
296   Print(startIt, startIt+count, options);      << 
297 }                                              << 
298                                                   216