Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4HnManager.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
29#include "G4HnManager.hh"
30#include "G4HnMessenger.hh"
31
33#include "G4VFileManager.hh"
34
35#include <utility>
36
37using namespace G4Analysis;
38using std::to_string;
39
40//_____________________________________________________________________________
42 : G4BaseAnalysisManager(state), fHnType(std::move(hnType))
43{}
44
45//_____________________________________________________________________________
47{
48 for ( auto info : fHnVector ) {
49 delete info;
50 }
51}
52
53//
54// private methods
55//
56
57//_____________________________________________________________________________
59{
60// Set activation to a given object
61
62 // Do nothing if activation does not change
63 if ( info->GetActivation() == activation ) return;
64
65 // Change activation and account it in fNofActiveObjects
66 info->SetActivation(activation);
67 if (activation) {
68 fNofActiveObjects++;
69 }
70 else {
71 fNofActiveObjects--;
72 }
73}
74
75//_____________________________________________________________________________
77{
78 // Do nothing if ascii does not change
79 if ( info->GetPlotting() == plotting ) return;
80
81 // Change Plotting and account it in fNofPlottingObjects
82 info->SetPlotting(plotting);
83 if (plotting) {
84 fNofPlottingObjects++;
85 }
86 else {
87 fNofPlottingObjects--;
88 }
89}
90
91//_____________________________________________________________________________
92void G4HnManager::SetFileName(G4HnInformation* info, const G4String& fileName)
93{
94 // Do nothing if file name does not change
95 if ( info->GetFileName() == fileName ) return;
96
97 auto hnFileName = fileName;
98 auto extension = GetExtension(fileName);
99 if (extension.size() != 0u) {
100 // Check if valid extension (if present)
101 auto output = G4Analysis::GetOutput(extension);
102 if ( output == G4AnalysisOutput::kNone ) {
103 Warn("The file extension " + extension + " is not supported.",
104 fkClass, "SetFileName");
105 return;
106 }
107 }
108 else {
109 if (fDefaultFileType.size() != 0u) {
110 //add extension if missing and file type is defined
111 hnFileName = fileName + "." + fDefaultFileType;
112 }
113 }
114
115 // Save the info and account a new file name if file manager
116 info->SetFileName(hnFileName);
117 if (fFileManager) {
118 fFileManager->AddFileName(hnFileName);
119 } else {
120 Warn("Failed to set fileName " + fileName +
121 " for object " + info->GetName() + ".\nFile manager is not set.",
122 fkClass, "SetFileName");
123 return;
124 }
125
126 if ( hnFileName != "" ) {
127 fNofFileNameObjects++;
128 } else {
129 fNofFileNameObjects--;
130 }
131}
132
133//
134// public methods
135//
136
137//_____________________________________________________________________________
139{
140 fMessenger = std::make_unique<G4HnMessenger>(*this);
141}
142
143//_____________________________________________________________________________
145{
146 // Add new information
147
148 fHnVector.push_back(info);
149 ++fNofActiveObjects;
150}
151
152//_____________________________________________________________________________
154{
155 // Replace the information at 'index' position with new one.
156 // Update new information with the previous one settings if the
157 // previous histogram was deleted with 'keepSetting' set true.
158
159 auto previousInfo = fHnVector[index];
160 if (previousInfo->GetDeletedPair().second) {
161 info->Update(*previousInfo);
162 }
163 delete previousInfo;
164
165 fHnVector[index] = info;
166
167 if (info->GetActivation()) { ++fNofActiveObjects; }
168 if (info->GetAscii()) { ++fNofAsciiObjects; }
169 if (info->GetPlotting()) { ++fNofPlottingObjects; }
170 if (! info->GetFileName().empty()) { ++fNofFileNameObjects; }
171}
172
173//_____________________________________________________________________________
175{
176 info->SetDeleted(true, keepSetting);
177
178 if (info->GetActivation()) { --fNofActiveObjects; }
179 if (info->GetAscii()) { --fNofAsciiObjects; }
180 if (info->GetPlotting()) { --fNofPlottingObjects; }
181 if (! info->GetFileName().empty()) { --fNofFileNameObjects; }
182}
183
184//_____________________________________________________________________________
186{
187 for ( auto info : fHnVector ) {
188 delete info;
189 }
190 fHnVector.clear();
191 SetLockFirstId(false);
192}
193
194//_____________________________________________________________________________
196 std::string_view functionName, G4bool warn) const
197{
198 G4int index = id - fFirstId;
199 if ( index < 0 || index >= G4int(fHnVector.size()) ) {
200 if ( warn ) {
201 Warn(fHnType + " histogram " + to_string(id) + " does not exist.",
202 fkClass, functionName);
203 }
204 return nullptr;
205 }
206 return fHnVector[index];
207}
208
209//_____________________________________________________________________________
211 G4int dimension,
212 std::string_view functionName, G4bool warn) const
213{
214 auto info = GetHnInformation(id, functionName, warn);
215 if (info == nullptr) return nullptr;
216
217 return info->GetHnDimensionInformation(dimension);
218}
219
220//_____________________________________________________________________________
222{
223 return ( fNofActiveObjects > 0 );
224}
225
226//_____________________________________________________________________________
228{
229 return ( fNofAsciiObjects > 0 );
230}
231
232//_____________________________________________________________________________
234{
235 return ( fNofPlottingObjects > 0 );
236}
237
238//_____________________________________________________________________________
240{
241 return ( fNofFileNameObjects > 0 );
242}
243
244//_____________________________________________________________________________
246{
247// Set activation to a given object
248
249 auto info = GetHnInformation(id, "SetActivation");
250
251 if (info == nullptr) return;
252
253 SetActivation(info, activation);
254}
255
256//_____________________________________________________________________________
258{
259// Set activation to all objects of the given type
260
261 //std::vector<G4HnInformation*>::iterator it;
262 //for ( it = fHnVector.begin(); it != fHnVector.end(); it++ ) {
263 // G4HnInformation* info = *it;
264
265 for ( auto info : fHnVector ) {
266 SetActivation(info, activation);
267 }
268}
269
270//_____________________________________________________________________________
272{
273 auto info = GetHnInformation(id, "SetAscii");
274
275 if (info == nullptr) return;
276
277 // Do nothing if ascii does not change
278 if ( info->GetAscii() == ascii ) return;
279
280 // Change ascii and account it in fNofAsciiObjects
281 info->SetAscii(ascii);
282 if (ascii) {
283 fNofAsciiObjects++;
284 }
285 else {
286 fNofAsciiObjects--;
287 }
288}
289
290//_____________________________________________________________________________
292{
293 auto info = GetHnInformation(id, "SetPlotting");
294
295 if (info == nullptr) return;
296
297 SetPlotting(info, plotting);
298}
299
300//_____________________________________________________________________________
302{
303// Set plotting to all objects of the given type
304
305 for ( auto info : fHnVector ) {
306 SetPlotting(info, plotting);
307 }
308}
309
310//_____________________________________________________________________________
311void G4HnManager::SetFileName(G4int id, const G4String& fileName)
312{
313 auto info = GetHnInformation(id, "SetFileName");
314
315 if (info == nullptr) return;
316
317 SetFileName(info, fileName);
318}
319
320//_____________________________________________________________________________
322{
323// Set plotting to all objects of the given type
324
325 for ( auto info : fHnVector ) {
326 SetFileName(info, fileName);
327 }
328}
329
330//_____________________________________________________________________________
331G4bool G4HnManager::SetAxisIsLog(unsigned int idim, G4int id, G4bool isLog)
332{
333 auto info = GetHnInformation(id, "SetAxisIsLog");
334
335 if (info == nullptr) return false;
336
337 info->SetIsLogAxis(idim, isLog);
338 return true;
339}
340
341//_____________________________________________________________________________
343{
344 auto info = GetHnInformation(id, "GetName");
345
346 if (info == nullptr) return "";
347
348 return info->GetName();
349}
350
351//_____________________________________________________________________________
352G4double G4HnManager::GetUnit(unsigned int idim, G4int id) const
353{
354 auto info = GetHnDimensionInformation(id, idim, "GetXUnit");
355
356 if (info == nullptr) return 1.0;
357
358 return info->fUnit;
359}
360
361//_____________________________________________________________________________
362G4bool G4HnManager::GetAxisIsLog(unsigned int idim, G4int id) const
363{
364 auto info = GetHnInformation(id, "GetXAxisIsLog");
365
366 if (info == nullptr) return false;
367
368 return info->GetIsLogAxis(idim);
369}
370
371//_____________________________________________________________________________
373{
374 auto info = GetHnInformation(id, "GetActivation");
375
376 if (info == nullptr) return true;
377
378 return info->GetActivation();
379}
380
381//_____________________________________________________________________________
383{
384 auto info = GetHnInformation(id, "GetAscii");
385
386 if (info == nullptr) return false;
387
388 return info->GetAscii();
389}
390
391//_____________________________________________________________________________
393{
394 auto info = GetHnInformation(id, "GetPlotting");
395
396 if (info == nullptr) return false;
397
398 return info->GetPlotting();
399}
400
401//_____________________________________________________________________________
403{
404 auto info = GetHnInformation(id, "GetFileName");
405
406 if (info == nullptr) return "";
407
408 return info->GetFileName();
409}
double G4double
Definition G4Types.hh:83
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
void SetLockFirstId(G4bool lockFirstId)
void SetAscii(G4bool ascii)
void SetPlotting(G4bool plotting)
G4bool GetIsLogAxis(G4int axis) const
void SetDeleted(G4bool deleted, G4bool keepSetting)
G4bool GetPlotting() const
G4bool GetActivation() const
G4bool GetAscii() const
G4String GetName() const
void SetIsLogAxis(G4int axis, G4bool isLog)
void Update(const G4HnInformation &other)
G4HnDimensionInformation * GetHnDimensionInformation(G4int dimension)
void SetActivation(G4bool activation)
G4String GetFileName() const
void SetFileName(const G4String &fileName)
G4HnDimensionInformation * GetHnDimensionInformation(G4int id, G4int dimension, std::string_view functionName, G4bool warn=true) const
void ClearData()
void CreateMessenger()
G4bool GetPlotting(G4int id) const
G4bool SetAxisIsLog(unsigned int idim, G4int id, G4bool isLogAxis)
void SetActivation(G4bool activation)
G4String GetFileName(G4int id) const
void SetHnDeleted(G4HnInformation *info, G4bool keepSetting)
G4String GetName(G4int id) const
G4bool IsAscii() const
~G4HnManager() override
G4bool IsPlotting() const
void SetFileName(G4int id, const G4String &fileName)
G4bool GetActivation(G4int id) const
void AddHnInformation(G4HnInformation *info)
G4bool GetAxisIsLog(unsigned int idim, G4int id) const
G4double GetUnit(unsigned int idim, G4int id) const
G4bool IsActive() const
void SetAscii(G4int id, G4bool ascii)
void SetPlotting(G4int id, G4bool plotting)
G4HnInformation * GetHnInformation(G4int id, std::string_view functionName, G4bool warn=true) const
G4HnManager()=delete
G4bool GetAscii(G4int id) const
G4bool IsFileName() const
G4String GetExtension(const G4String &fileName, const G4String &defaultExtension="")
G4AnalysisOutput GetOutput(const G4String &outputName, G4bool warn=true)
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)