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