Geant4-11
G4H1ToolsManager.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 (ivana@ipno.in2p3.fr)
28
29#include "G4H1ToolsManager.hh"
33
34#include "tools/histo/h1d"
35
36#include <fstream>
37
38using namespace G4Analysis;
39using std::to_string;
40
41//
42// Constructors, destructor
43//
44
45//_____________________________________________________________________________
47 : G4VH1Manager(),
48 G4THnManager<tools::histo::h1d>(state, "H1")
49{}
50
51//
52// Utility functions
53//
54
55
56namespace {
57
58//_____________________________________________________________________________
60 const G4String& unitName,
61 const G4String& fcnName,
62 G4BinScheme binScheme)
63{
64 hnInformation->SetDimension(kX, unitName, fcnName, binScheme);
65}
66
67//_____________________________________________________________________________
68void AddH1Annotation(tools::histo::h1d* h1d,
69 const G4String& unitName,
70 const G4String& fcnName)
71{
72 G4String axisTitle;
73 UpdateTitle(axisTitle, unitName, fcnName);
74 h1d->add_annotation(tools::histo::key_axis_x_title(), axisTitle);
75}
76
77//_____________________________________________________________________________
78tools::histo::h1d* CreateToolsH1(const G4String& title,
79 G4int nbins, G4double xmin, G4double xmax,
80 const G4String& unitName,
81 const G4String& fcnName,
82 const G4String& binSchemeName,
83 std::string_view className)
84{
85 auto unit = GetUnitValue(unitName);
86 auto fcn = GetFunction(fcnName);
87 auto binScheme = GetBinScheme(binSchemeName);
88
89 if ( binScheme != G4BinScheme::kLog ) {
90 if ( binScheme == G4BinScheme::kUser ) {
91 // This should never happen, but let's make sure about it
92 // by issuing a warning
93 Warn("User binning scheme setting was ignored.\n"
94 "Linear binning will be applied with given (nbins, xmin, xmax) values.",
95 className, "CreateToolsH1");
96 }
97 return new tools::histo::h1d(title, nbins, fcn(xmin/unit), fcn(xmax/unit));
98 }
99 else {
100 // Compute edges
101 std::vector<G4double> edges;
102 ComputeEdges(nbins, xmin, xmax, unit, fcn, binScheme, edges);
103 return new tools::histo::h1d(title, edges);
104 }
105}
106
107//_____________________________________________________________________________
108tools::histo::h1d* CreateToolsH1(const G4String& title,
109 const std::vector<G4double>& edges,
110 const G4String& unitName,
111 const G4String& fcnName)
112{
113 auto unit = GetUnitValue(unitName);
114 auto fcn = GetFunction(fcnName);
115
116 // Apply function
117 std::vector<G4double> newEdges;
118 ComputeEdges(edges, unit, fcn, newEdges);
119
120 return new tools::histo::h1d(title, newEdges);
121}
122
123//_____________________________________________________________________________
124void ConfigureToolsH1(tools::histo::h1d* h1d,
125 G4int nbins, G4double xmin, G4double xmax,
126 const G4String& unitName,
127 const G4String& fcnName,
128 const G4String& binSchemeName,
129 std::string_view className)
130{
131 auto unit = GetUnitValue(unitName);
132 auto fcn = GetFunction(fcnName);
133 auto binScheme = GetBinScheme(binSchemeName);
134
135 if ( binScheme != G4BinScheme::kLog ) {
136 if ( binScheme == G4BinScheme::kUser ) {
137 // This should never happen, but let's make sure about it
138 // by issuing a warning
139 Warn("User binning scheme setting was ignored.\n"
140 "Linear binning will be applied with given (nbins, xmin, xmax) values.",
141 className, "ConfigureToolsH1");
142
143 }
144 h1d->configure(nbins, fcn(xmin/unit), fcn(xmax/unit));
145 }
146 else {
147 // Compute bins
148 std::vector<G4double> edges;
149 ComputeEdges(nbins, xmin, xmax, unit, fcn, binScheme, edges);
150 h1d->configure(edges);
151 }
152}
153
154//_____________________________________________________________________________
155void ConfigureToolsH1(tools::histo::h1d* h1d,
156 const std::vector<G4double>& edges,
157 const G4String& unitName,
158 const G4String& fcnName)
159{
160 // Apply function to edges
161 auto unit = GetUnitValue(unitName);
162 auto fcn = GetFunction(fcnName);
163 std::vector<G4double> newEdges;
164 ComputeEdges(edges, unit, fcn, newEdges);
165
166 h1d->configure(newEdges);
167}
168
169}
170
171//
172// private methods
173//
174
175//_____________________________________________________________________________
177 const G4String& unitName,
178 const G4String& fcnName,
179 G4BinScheme binScheme) const
180{
181 auto hnInformation = fHnManager->AddHnInformation(name, fkDimension);
182 hnInformation->AddDimension(unitName, fcnName, binScheme);
183}
184
185//
186// protected methods
187//
188
189//_____________________________________________________________________________
191 G4int nbins, G4double xmin, G4double xmax,
192 const G4String& unitName, const G4String& fcnName,
193 const G4String& binSchemeName)
194{
195 Message(kVL4, "create", "H1", name);
196
197 // Create H1
198 auto h1d
199 = CreateToolsH1(title, nbins, xmin, xmax, unitName, fcnName, binSchemeName,
200 fkClass);
201
202 // Add annotation
203 AddH1Annotation(h1d, unitName, fcnName);
204
205 // Save H1 information
206 auto binScheme = GetBinScheme(binSchemeName);
207 AddH1Information(name, unitName, fcnName, binScheme);
208
209 // Register histogram
210 auto id = RegisterT(h1d, name);
211
212 Message(kVL2, "create", "H1", name);
213
214 return id;
215}
216
217//_____________________________________________________________________________
219 const std::vector<G4double>& edges,
220 const G4String& unitName, const G4String& fcnName)
221{
222 Message(kVL4, "create", "H1", name);
223
224 auto h1d
225 = CreateToolsH1(title, edges, unitName, fcnName);
226
227 // Add annotation
228 AddH1Annotation(h1d, unitName, fcnName);
229
230 // Save H1 information
231 AddH1Information(name, unitName, fcnName, G4BinScheme::kUser);
232
233 // Register histogram
234 auto id = RegisterT(h1d, name);
235
236 Message(kVL2, "create", "H1", name);
237
238 return id;
239}
240
241//_____________________________________________________________________________
243 G4int nbins, G4double xmin, G4double xmax,
244 const G4String& unitName, const G4String& fcnName,
245 const G4String& binSchemeName)
246{
247 auto h1d = GetTInFunction(id, "SetH1", false, false);
248 if ( ! h1d ) return false;
249
250 auto info = fHnManager->GetHnInformation(id,"SetH1");
251
252 Message(kVL4, "configure", "H1", info->GetName());
253
254 // Configure tools h1
255 ConfigureToolsH1(h1d, nbins, xmin, xmax, unitName, fcnName, binSchemeName,
256 fkClass);
257
258 // Add annotation
259 AddH1Annotation(h1d, unitName, fcnName);
260
261 // Update information
262 auto binScheme = GetBinScheme(binSchemeName);
263 UpdateH1Information(info, unitName, fcnName, binScheme);
264
265 // Set activation
266 fHnManager->SetActivation(id, true);
267
268 return true;
269}
270
271//_____________________________________________________________________________
273 const std::vector<G4double>& edges,
274 const G4String& unitName,
275 const G4String& fcnName)
276{
277 auto h1d = GetTInFunction(id, "SetH1", false, false);
278 if ( ! h1d ) return false;
279
280 auto info = fHnManager->GetHnInformation(id,"SetH1");
281
282 Message(kVL4, "configure", "H1", info->GetName());
283
284 // Configure tools h1
285 ConfigureToolsH1(h1d, edges, unitName, fcnName);
286
287 // Add annotation
288 AddH1Annotation(h1d, unitName, fcnName);
289
290 // Update information
291 UpdateH1Information(info, unitName, fcnName, G4BinScheme::kUser);
292
293 // Set activation
294 fHnManager->SetActivation(id, true);
295
296 return true;
297}
298
299
300//_____________________________________________________________________________
302{
303 auto h1d = GetTInFunction(id, "ScaleH1", false, false);
304 if ( ! h1d ) return false;
305
306 return h1d->scale(factor);
307}
308
309//_____________________________________________________________________________
311{
312 auto h1d = GetTInFunction(id, "FillH1", true, false);
313 if ( ! h1d ) return false;
314
315 if ( fState.GetIsActivation() && ( ! fHnManager->GetActivation(id) ) ) {
316 //G4cout << "Skipping FillH1 for " << id << G4endl;
317 return false;
318 }
319
320 auto info
321 = fHnManager->GetHnDimensionInformation(id, kX, "FillH1");
322 h1d->fill(info->fFcn(value/info->fUnit), weight);
323
324 if ( IsVerbose(kVL4) ) {
325 Message(kVL4, "fill", "H1",
326 " id " + to_string(id) + " value " + to_string(value) +
327 " fcn(value/unit) " + to_string(info->fFcn(value/info->fUnit)) +
328 " weight " + to_string(weight));
329 }
330
331 return true;
332}
333
334//_____________________________________________________________________________
336{
337 return GetTId(name, warn);
338}
339
340//_____________________________________________________________________________
342{
343 auto h1d = GetTInFunction(id, "GetH1Nbins");
344 if ( ! h1d ) return 0;
345
346 return GetNbins(*h1d, kX);
347}
348
349//_____________________________________________________________________________
351{
352// Returns xmin value with applied unit and histogram function
353
354 auto h1d = GetTInFunction(id, "GetH1Xmin");
355 if ( ! h1d ) return 0.;
356
357 return GetMin(*h1d, kX);
358}
359
360//_____________________________________________________________________________
362{
363 auto h1d = GetTInFunction(id, "GetH1Xmax");
364 if ( ! h1d ) return 0.;
365
366 return GetMax(*h1d, kX);
367}
368
369//_____________________________________________________________________________
371{
372 auto h1d = GetTInFunction(id, "GetH1XWidth", true, false);
373 if ( ! h1d ) return 0.;
374
375 return GetWidth(*h1d, kX, fHnManager->GetHnType());
376}
377
378//_____________________________________________________________________________
380{
381 auto h1d = GetTInFunction(id, "SetH1Title");
382 if ( ! h1d ) return false;
383
384 return SetTitle(*h1d, title);
385}
386
387//_____________________________________________________________________________
389{
390 auto h1d = GetTInFunction(id, "SetH1XAxisTitle");
391 if ( ! h1d ) return false;
392
393 return SetAxisTitle(*h1d, kX, title);
394}
395
396//_____________________________________________________________________________
398{
399 auto h1d = GetTInFunction(id, "SetH1YAxisTitle");
400 if ( ! h1d ) return false;
401
402 return SetAxisTitle(*h1d, kY, title);
403}
404
405//_____________________________________________________________________________
407{
408 auto h1d = GetTInFunction(id, "GetH1Title");
409 if ( ! h1d ) return "";
410
411 return GetTitle(*h1d);
412}
413
414
415//_____________________________________________________________________________
417{
418 auto h1d = GetTInFunction(id, "GetH1XAxisTitle");
419 if ( ! h1d ) return "";
420
421 return GetAxisTitle(*h1d, kX, fHnManager->GetHnType());
422}
423
424//_____________________________________________________________________________
426{
427 auto h1d = GetTInFunction(id, "GetH1YAxisTitle");
428 if ( ! h1d ) return "";
429
430 return GetAxisTitle(*h1d, kY, fHnManager->GetHnType());
431}
432
433//_____________________________________________________________________________
435{
436// Write selected objects on ASCII file
437// According to the implementation by Michel Maire, originally in
438// extended examples.
439
440 // Do nothing if no histograms are selected
441 if ( ! fHnManager->IsAscii() ) return true;
442
443 // Write h1 histograms
444 for ( G4int i=0; i<G4int(fTVector.size()); ++i ) {
445 auto id = i + fHnManager->GetFirstId();
446 auto info = fHnManager->GetHnInformation(id,"WriteOnAscii");
447 // skip writing if activation is enabled and H1 is inactivated
448 if ( ! info->GetAscii() ) continue;
449 auto h1 = fTVector[i];
450
451 Message(kVL3, "write on ascii", "h1d", info->GetName());
452
453 output << "\n 1D histogram " << id << ": " << h1->title()
454 << "\n \n \t X \t\t Bin Height" << G4endl;
455
456 for (G4int j=0; j< G4int(h1->axis().bins()); ++j) {
457 output << " " << j << "\t"
458 << h1->axis().bin_center(j) << "\t"
459 << h1->bin_height(j) << G4endl;
460 }
461 }
462
463 return output.good();
464}
465
466//
467// public methods
468//
469
470//_____________________________________________________________________________
471G4int G4H1ToolsManager::AddH1(const G4String& name, tools::histo::h1d* h1d)
472{
473 Message(kVL4, "add", "H1", name);
474
475 // Add annotation
476 AddH1Annotation(h1d, "none", "none");
477 // Add information
479
480 // Register histogram
481 auto id = RegisterT(h1d, name);
482
483 Message(kVL2, "add", "H1", name);
484
485 return id;
486}
487
488
489//_____________________________________________________________________________
491 const std::vector<tools::histo::h1d*>& h1Vector)
492{
493 AddTVector(h1Vector);
494}
495
496//_____________________________________________________________________________
497tools::histo::h1d* G4H1ToolsManager::GetH1(G4int id, G4bool warn,
498 G4bool onlyIfActive) const
499{
500 return GetTInFunction(id, "GetH1", warn, onlyIfActive);
501}
502
G4BinScheme
Definition: G4BinScheme.hh:39
double G4double
Definition: G4Types.hh:83
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
static char className[]
Definition: G4Win32.cc:36
#define G4endl
Definition: G4ios.hh:57
virtual G4int GetH1Id(const G4String &name, G4bool warn=true) const final
virtual G4String GetH1YAxisTitle(G4int id) const final
tools::histo::h1d * GetH1(G4int id, G4bool warn=true, G4bool onlyIfActive=true) const
virtual G4int GetH1Nbins(G4int id) const final
virtual G4String GetH1XAxisTitle(G4int id) const final
virtual G4bool SetH1YAxisTitle(G4int id, const G4String &title) final
virtual G4bool WriteOnAscii(std::ofstream &output) final
virtual G4bool SetH1(G4int id, G4int nbins, G4double xmin, G4double xmax, const G4String &unitName="none", const G4String &fcnName="none", const G4String &binSchemeName="linear") final
void AddH1Information(const G4String &name, const G4String &unitName, const G4String &fcnName, G4BinScheme binScheme) const
virtual G4double GetH1Width(G4int id) const final
virtual G4bool SetH1XAxisTitle(G4int id, const G4String &title) final
virtual G4String GetH1Title(G4int id) const final
G4int AddH1(const G4String &name, tools::histo::h1d *h1d)
static constexpr G4int fkDimension
virtual G4int CreateH1(const G4String &name, const G4String &title, G4int nbins, G4double xmin, G4double xmax, const G4String &unitName="none", const G4String &fcnName="none", const G4String &binScheme="linear") final
virtual G4bool ScaleH1(G4int id, G4double factor) final
virtual G4bool SetH1Title(G4int id, const G4String &title) final
static constexpr std::string_view fkClass
virtual G4double GetH1Xmin(G4int id) const final
void AddH1Vector(const std::vector< tools::histo::h1d * > &h1Vector)
virtual G4bool FillH1(G4int id, G4double value, G4double weight=1.0) final
virtual G4double GetH1Xmax(G4int id) const final
G4H1ToolsManager()=delete
void SetDimension(G4int dimension, const G4String &unitName, const G4String &fcnName, G4BinScheme binScheme)
std::vector< tools::histo::h1d * > fTVector
tools::histo::h1d * GetTInFunction(G4int id, std::string_view functionName, G4bool warn=true, G4bool onlyIfActive=true) const
const G4AnalysisManagerState & fState
G4int GetTId(const G4String &name, G4bool warn=true) const
G4int RegisterT(tools::histo::h1d *t, const G4String &name)
void AddTVector(const std::vector< tools::histo::h1d * > &tVector)
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
std::shared_ptr< G4HnManager > fHnManager
G4bool IsVerbose(G4int verboseLevel) const
G4int GetNbins(const G4ToolsBaseHisto &baseHisto, G4int dimension)
G4BinScheme GetBinScheme(const G4String &binSchemeName)
Definition: G4BinScheme.cc:36
G4bool SetTitle(G4ToolsBaseHisto &baseHisto, const G4String &title)
G4double GetMin(const G4ToolsBaseHisto &baseHisto, G4int dimension)
G4double GetMax(const G4ToolsBaseHisto &baseHisto, G4int dimension)
constexpr G4int kVL2
G4double GetUnitValue(const G4String &unit)
constexpr G4int kVL3
void ComputeEdges(G4int nbins, G4double xmin, G4double xmax, G4double unit, G4Fcn fcn, G4BinScheme, std::vector< G4double > &edges)
Definition: G4BinScheme.cc:53
G4bool SetAxisTitle(G4ToolsBaseHisto &baseHisto, G4int dimension, const G4String &title)
constexpr G4int kVL4
G4String GetAxisTitle(const G4ToolsBaseHisto &baseHisto, G4int dimension, const G4String &hnType)
G4double GetWidth(const G4ToolsBaseHisto &baseHisto, G4int dimension, const G4String &hnType)
void UpdateTitle(G4String &title, const G4String &unitName, const G4String &fcnName)
constexpr G4int kX
G4String GetTitle(const G4ToolsBaseHisto &baseHisto)
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)
G4Fcn GetFunction(const G4String &fcnName)
Definition: G4Fcn.cc:36
constexpr G4int kY
const char * name(G4int ptype)
tools::histo::h1d * CreateToolsH1(const G4String &title, const std::vector< G4double > &edges, const G4String &unitName, const G4String &fcnName)
void AddH1Annotation(tools::histo::h1d *h1d, const G4String &unitName, const G4String &fcnName)
void UpdateH1Information(G4HnInformation *hnInformation, const G4String &unitName, const G4String &fcnName, G4BinScheme binScheme)
void ConfigureToolsH1(tools::histo::h1d *h1d, const std::vector< G4double > &edges, const G4String &unitName, const G4String &fcnName)