Geant4  10.00.p03
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"
31 #include "G4HnManager.hh"
33 #include "G4AnalysisUtilities.hh"
34 
35 #include "tools/histo/h1d"
36 
37 #include <fstream>
38 
39 using namespace G4Analysis;
40 
41 //
42 // Constructors, destructor
43 //
44 
45 //_____________________________________________________________________________
47  : G4VH1Manager(state),
48  fH1Vector(),
49  fH1NameIdMap()
50 {
51 }
52 
53 //_____________________________________________________________________________
55 {
56  std::vector<tools::histo::h1d*>::iterator it;
57  for (it = fH1Vector.begin(); it != fH1Vector.end(); it++ ) {
58  delete (*it);
59  }
60 }
61 
62 //
63 // Utility functions
64 //
65 
66 namespace {
67 
68 //_____________________________________________________________________________
69 void UpdateH1Information(G4HnInformation* information,
70  const G4String& unitName,
71  const G4String& fcnName,
72  G4BinScheme binScheme)
73 {
74  G4double unit = GetUnitValue(unitName);
75  G4Fcn fcn = GetFunction(fcnName);
76  information->fXUnitName = unitName;
77  information->fYUnitName = unitName;
78  information->fXFcnName = fcnName;
79  information->fYFcnName = fcnName;
80  information->fXUnit = unit;
81  information->fYUnit = unit;
82  information->fXFcn = fcn;
83  information->fYFcn = fcn;
84  information->fXBinScheme = binScheme;
85  information->fYBinScheme = binScheme;
86 }
87 
88 //_____________________________________________________________________________
89 void AddH1Annotation(tools::histo::h1d* h1d,
90  const G4String& unitName,
91  const G4String& fcnName)
92 {
93  G4String axisTitle;
94  UpdateTitle(axisTitle, unitName, fcnName);
95  h1d->add_annotation(tools::histo::key_axis_x_title(), axisTitle);
96 }
97 
98 //_____________________________________________________________________________
99 tools::histo::h1d* CreateToolsH1(const G4String& title,
100  G4int nbins, G4double xmin, G4double xmax,
101  const G4String& unitName,
102  const G4String& fcnName,
103  const G4String& binSchemeName)
104 {
105  G4double unit = GetUnitValue(unitName);
106  G4Fcn fcn = GetFunction(fcnName);
107  G4BinScheme binScheme = GetBinScheme(binSchemeName);
108 
109  if ( binScheme != kLogBinScheme ) {
110  if ( binScheme == kUserBinScheme ) {
111  // This should never happen, but let's make sure about it
112  // by issuing a warning
113  G4ExceptionDescription description;
114  description
115  << " User binning scheme setting was ignored." << G4endl
116  << " Linear binning will be applied with given (nbins, xmin, xmax) values";
117  G4Exception("G4H1ToolsManager::CreateH1",
118  "Analysis_W013", JustWarning, description);
119  }
120  return new tools::histo::h1d(title, nbins, fcn(xmin/unit), fcn(xmax/unit));
121  }
122  else {
123  // Compute edges
124  std::vector<G4double> edges;
125  ComputeEdges(nbins, xmin, xmax, unit, fcn, binScheme, edges);
126  return new tools::histo::h1d(title, edges);
127  }
128 }
129 
130 //_____________________________________________________________________________
131 tools::histo::h1d* CreateToolsH1(const G4String& title,
132  const std::vector<G4double>& edges,
133  const G4String& unitName,
134  const G4String& fcnName)
135 {
136  G4double unit = GetUnitValue(unitName);
137  G4Fcn fcn = GetFunction(fcnName);
138 
139  // Apply function
140  std::vector<G4double> newEdges;
141  ComputeEdges(edges, unit, fcn, newEdges);
142 
143  return new tools::histo::h1d(title, newEdges);
144 }
145 
146 //_____________________________________________________________________________
147 void ConfigureToolsH1(tools::histo::h1d* h1d,
148  G4int nbins, G4double xmin, G4double xmax,
149  const G4String& unitName,
150  const G4String& fcnName,
151  const G4String& binSchemeName)
152 {
153  G4double unit = GetUnitValue(unitName);
154  G4Fcn fcn = GetFunction(fcnName);
155  G4BinScheme binScheme = GetBinScheme(binSchemeName);
156 
157  if ( binScheme != kLogBinScheme ) {
158  if ( binScheme == kUserBinScheme ) {
159  // This should never happen, but let's make sure about it
160  // by issuing a warning
161  G4ExceptionDescription description;
162  description
163  << " User binning scheme setting was ignored." << G4endl
164  << " Linear binning will be applied with given (nbins, xmin, xmax) values";
165  G4Exception("G4H1ToolsManager::SetH1",
166  "Analysis_W013", JustWarning, description);
167  }
168  h1d->configure(nbins, fcn(xmin/unit), fcn(xmax/unit));
169  }
170  else {
171  // Compute bins
172  std::vector<G4double> edges;
173  ComputeEdges(nbins, xmin, xmax, unit, fcn, binScheme, edges);
174  h1d->configure(edges);
175  }
176 }
177 
178 //_____________________________________________________________________________
179 void ConfigureToolsH1(tools::histo::h1d* h1d,
180  const std::vector<G4double>& edges,
181  const G4String& unitName,
182  const G4String& fcnName)
183 {
184  // Apply function to edges
185  G4double unit = GetUnitValue(unitName);
186  G4Fcn fcn = GetFunction(fcnName);
187  std::vector<G4double> newEdges;
188  ComputeEdges(edges, unit, fcn, newEdges);
189 
190  h1d->configure(newEdges);
191 }
192 
193 }
194 
195 //
196 // private methods
197 //
198 
199 //_____________________________________________________________________________
201  G4String functionName, G4bool warn,
202  G4bool onlyIfActive) const
203 {
204  G4int index = id - fFirstId;
205  if ( index < 0 || index >= G4int(fH1Vector.size()) ) {
206  if ( warn) {
207  G4String inFunction = "G4H1ToolsManager::";
208  inFunction += functionName;
209  G4ExceptionDescription description;
210  description << " " << "histogram " << id << " does not exist.";
211  G4Exception(inFunction, "Analysis_W007", JustWarning, description);
212  }
213  return 0;
214  }
215 
216  // Do not return histogram if inactive
217  if ( fState.GetIsActivation() && onlyIfActive && ( ! fHnManager->GetActivation(id) ) ) {
218  return 0;
219  }
220 
221  return fH1Vector[index];
222 }
223 
224 //_____________________________________________________________________________
226  const G4String& unitName,
227  const G4String& fcnName,
228  G4BinScheme binScheme) const
229 {
230  G4double unit = GetUnitValue(unitName);
231  G4Fcn fcn = GetFunction(fcnName);
232  fHnManager->AddH1Information(name, unitName, fcnName, unit, fcn, binScheme);
233 }
234 
235 //_____________________________________________________________________________
236 G4int G4H1ToolsManager::RegisterToolsH1(tools::histo::h1d* h1d,
237  const G4String& name)
238 {
239  G4int index = fH1Vector.size();
240  fH1Vector.push_back(h1d);
241 
242  fLockFirstId = true;
243  fH1NameIdMap[name] = index + fFirstId;
244  return index + fFirstId;
245 }
246 
247 //
248 // protected methods
249 //
250 
251 //_____________________________________________________________________________
253  G4int nbins, G4double xmin, G4double xmax,
254  const G4String& unitName, const G4String& fcnName,
255  const G4String& binSchemeName)
256 {
257 #ifdef G4VERBOSE
258  if ( fState.GetVerboseL4() )
259  fState.GetVerboseL4()->Message("create", "H1", name);
260 #endif
261  tools::histo::h1d* h1d
262  = CreateToolsH1(title, nbins, xmin, xmax, unitName, fcnName, binSchemeName);
263 
264  // Add annotation
265  AddH1Annotation(h1d, unitName, fcnName);
266 
267  // Save H1 information
268  G4BinScheme binScheme = GetBinScheme(binSchemeName);
269  AddH1Information(name, unitName, fcnName, binScheme);
270 
271  // Register histogram
272  G4int id = RegisterToolsH1(h1d, name);
273 
274 #ifdef G4VERBOSE
275  if ( fState.GetVerboseL2() )
276  fState.GetVerboseL2()->Message("create", "H1", name);
277 #endif
278  return id;
279 }
280 
281 //_____________________________________________________________________________
283  const std::vector<G4double>& edges,
284  const G4String& unitName, const G4String& fcnName)
285 {
286 #ifdef G4VERBOSE
287  if ( fState.GetVerboseL4() )
288  fState.GetVerboseL4()->Message("create", "H1", name);
289 #endif
290  tools::histo::h1d* h1d
291  = CreateToolsH1(title, edges, unitName, fcnName);
292 
293  // Add annotation
294  AddH1Annotation(h1d, unitName, fcnName);
295 
296  // Save H1 information
297  AddH1Information(name, unitName, fcnName, kUserBinScheme);
298 
299  // Register histogram
300  G4int id = RegisterToolsH1(h1d, name);
301 
302 #ifdef G4VERBOSE
303  if ( fState.GetVerboseL2() )
304  fState.GetVerboseL2()->Message("create", "H1", name);
305 #endif
306  return id;
307 }
308 
309 //_____________________________________________________________________________
311  G4int nbins, G4double xmin, G4double xmax,
312  const G4String& unitName, const G4String& fcnName,
313  const G4String& binSchemeName)
314 {
315  tools::histo::h1d* h1d = GetH1InFunction(id, "SetH1", false, false);
316  if ( ! h1d ) return false;
317 
318  G4HnInformation* info = fHnManager->GetHnInformation(id,"SetH1");
319 #ifdef G4VERBOSE
320  if ( fState.GetVerboseL4() )
321  fState.GetVerboseL4()->Message("configure", "H1", info->fName);
322 #endif
323 
324  // Configure tools h1
325  ConfigureToolsH1(h1d, nbins, xmin, xmax, unitName, fcnName, binSchemeName);
326 
327  // Add annotation
328  AddH1Annotation(h1d, unitName, fcnName);
329 
330  // Update information
331  G4BinScheme binScheme = GetBinScheme(binSchemeName);
332  UpdateH1Information(info, unitName, fcnName, binScheme);
333 
334  // Set activation
335  fHnManager->SetActivation(id, true);
336 
337  return true;
338 }
339 
340 //_____________________________________________________________________________
342  const std::vector<G4double>& edges,
343  const G4String& unitName,
344  const G4String& fcnName)
345 {
346  tools::histo::h1d* h1d = GetH1InFunction(id, "SetH1", false, false);
347  if ( ! h1d ) return false;
348 
349  G4HnInformation* info = fHnManager->GetHnInformation(id,"SetH1");
350 #ifdef G4VERBOSE
351  if ( fState.GetVerboseL4() )
352  fState.GetVerboseL4()->Message("configure", "H1", info->fName);
353 #endif
354 
355  // Configure tools h1
356  ConfigureToolsH1(h1d, edges, unitName, fcnName);
357 
358  // Add annotation
359  AddH1Annotation(h1d, unitName, fcnName);
360 
361  // Update information
362  UpdateH1Information(info, unitName, fcnName, kUserBinScheme);
363 
364  // Set activation
365  fHnManager->SetActivation(id, true);
366 
367  return true;
368 }
369 
370 
371 //_____________________________________________________________________________
373 {
374  tools::histo::h1d* h1d = GetH1InFunction(id, "ScaleH1", false, false);
375  if ( ! h1d ) return false;
376 
377  return h1d->scale(factor);
378 }
379 
380 //_____________________________________________________________________________
382 {
383  tools::histo::h1d* h1d = GetH1InFunction(id, "FillH1", true, false);
384  if ( ! h1d ) return false;
385 
386  if ( fState.GetIsActivation() && ( ! fHnManager->GetActivation(id) ) ) {
387  //G4cout << "Skipping FillH1 for " << id << G4endl;
388  return false;
389  }
390 
391  G4HnInformation* info = fHnManager->GetHnInformation(id, "FillId");
392  h1d->fill(info->fXFcn(value/info->fXUnit), weight);
393 #ifdef G4VERBOSE
394  if ( fState.GetVerboseL4() ) {
395  G4ExceptionDescription description;
396  description << " id " << id << " value " << value;
397  fState.GetVerboseL4()->Message("fill", "H1", description);
398  }
399 #endif
400  return true;
401 }
402 
403 //_____________________________________________________________________________
405 {
406  std::map<G4String, G4int>::const_iterator it = fH1NameIdMap.find(name);
407  if ( it == fH1NameIdMap.end() ) {
408  if ( warn) {
409  G4String inFunction = "G4H1ToolsManager::GetH1Id";
410  G4ExceptionDescription description;
411  description << " " << "histogram " << name << " does not exist.";
412  G4Exception(inFunction, "Analysis_W007", JustWarning, description);
413  }
414  return -1;
415  }
416  return it->second;
417 }
418 
419 //_____________________________________________________________________________
421 {
422  tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1Nbins");
423  if ( ! h1d ) return 0;
424 
425  return h1d->axis().bins();
426 }
427 
428 //_____________________________________________________________________________
430 {
431 // Returns xmin value with applied unit and histogram function
432 
433  tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1Xmin");
434  if ( ! h1d ) return 0;
435 
436  return h1d->axis().lower_edge();
437 }
438 
439 //_____________________________________________________________________________
441 {
442  tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1Xmax");
443  if ( ! h1d ) return 0;
444 
445  return h1d->axis().upper_edge();
446 }
447 
448 //_____________________________________________________________________________
450 {
451  tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1XWidth", true, false);
452  if ( ! h1d ) return 0;
453 
454  G4int nbins = h1d->axis().bins();
455  if ( ! nbins ) {
456  G4ExceptionDescription description;
457  description << " nbins = 0 (for h1 id = " << id << ").";
458  G4Exception("G4H1ToolsManager::GetH1Width",
459  "Analysis_W014", JustWarning, description);
460  return 0;
461  }
462 
463  return ( h1d->axis().upper_edge() - h1d->axis().lower_edge())/nbins;
464 }
465 
466 //_____________________________________________________________________________
468 {
469  tools::histo::h1d* h1d = GetH1InFunction(id, "SetH1Title");
470  if ( ! h1d ) return false;
471 
472  return h1d->set_title(title);
473 }
474 
475 //_____________________________________________________________________________
477 {
478  tools::histo::h1d* h1d = GetH1InFunction(id, "SetH1XAxisTitle");
479  if ( ! h1d ) return false;
480 
481  h1d->add_annotation(tools::histo::key_axis_x_title(), title);
482  return true;
483 }
484 
485 //_____________________________________________________________________________
487 {
488  tools::histo::h1d* h1d = GetH1InFunction(id, "SetH1YAxisTitle");
489  if ( ! h1d ) return false;
490 
491  h1d->add_annotation(tools::histo::key_axis_y_title(), title);
492  return true;
493 }
494 
495 //_____________________________________________________________________________
497 {
498  tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1Title");
499  if ( ! h1d ) return "";
500 
501  return h1d->title();
502 }
503 
504 
505 //_____________________________________________________________________________
507 {
508  tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1XAxisTitle");
509  if ( ! h1d ) return "";
510 
511  G4String title;
512  G4bool result = h1d->annotation(tools::histo::key_axis_x_title(), title);
513  if ( ! result ) {
514  G4ExceptionDescription description;
515  description << " Failed to get x_axis title for h1 id = " << id << ").";
516  G4Exception("G4H1ToolsManager::GetH1XAxisTitle",
517  "Analysis_W014", JustWarning, description);
518  return "";
519  }
520 
521  return title;
522 }
523 
524 //_____________________________________________________________________________
526 {
527  tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1YAxisTitle");
528  if ( ! h1d ) return "";
529 
530  G4String title;
531  G4bool result = h1d->annotation(tools::histo::key_axis_y_title(), title);
532  if ( ! result ) {
533  G4ExceptionDescription description;
534  description << " Failed to get y_axis title for h1 id = " << id << ").";
535  G4Exception("G4H1ToolsManager::GetH1YAxisTitle",
536  "Analysis_W014", JustWarning, description);
537  return "";
538  }
539 
540  return title;
541 }
542 
543 //_____________________________________________________________________________
545 {
546 // Write selected objects on ASCII file
547 // According to the implementation by Michel Maire, originally in
548 // extended examples.
549 
550  // h1 histograms
551  for ( G4int i=0; i<G4int(fH1Vector.size()); ++i ) {
552  G4int id = i + fFirstId;
553  G4HnInformation* info = fHnManager->GetHnInformation(id,"WriteOnAscii");
554  // skip writing if activation is enabled and H1 is inactivated
555  if ( ! info->fAscii ) continue;
556  tools::histo::h1d* h1 = fH1Vector[i];
557 
558 #ifdef G4VERBOSE
559  if ( fState.GetVerboseL3() )
560  fState.GetVerboseL3()->Message("write on ascii", "h1d", info->fName);
561 #endif
562 
563  output << "\n 1D histogram " << id << ": " << h1->title()
564  << "\n \n \t X \t\t Y" << G4endl;
565 
566  for (G4int j=0; j< G4int(h1->axis().bins()); ++j) {
567  output << " " << j << "\t"
568  << h1->axis().bin_center(j) << "\t"
569  << h1->bin_height(j) << G4endl;
570  }
571  }
572 
573  return true;
574 }
575 
576 //
577 // public methods
578 //
579 
580 //_____________________________________________________________________________
582  const std::vector<tools::histo::h1d*>& h1Vector)
583 {
584 #ifdef G4VERBOSE
585  if ( fState.GetVerboseL4() )
586  fState.GetVerboseL4()->Message("merge", "all h1", "");
587 #endif
588  std::vector<tools::histo::h1d*>::const_iterator itw = h1Vector.begin();
589  std::vector<tools::histo::h1d*>::iterator it;
590  for (it = fH1Vector.begin(); it != fH1Vector.end(); it++ ) {
591  (*it)->add(*(*itw++));
592  }
593 #ifdef G4VERBOSE
594  if ( fState.GetVerboseL1() )
595  fState.GetVerboseL1()->Message("merge", "all h1", "");
596 #endif
597 }
598 
599 //_____________________________________________________________________________
601 {
602 // Reset histograms and ntuple
603 
604  G4bool finalResult = true;
605 
606  std::vector<tools::histo::h1d*>::iterator it;
607  for (it = fH1Vector.begin(); it != fH1Vector.end(); it++ ) {
608  G4bool result = (*it)->reset();
609  if ( ! result ) finalResult = false;
610  }
611 
612  return finalResult;
613 }
614 
615 //_____________________________________________________________________________
617 {
618  return ! fH1Vector.size();
619 }
620 
621 //_____________________________________________________________________________
622 tools::histo::h1d* G4H1ToolsManager::GetH1(G4int id, G4bool warn,
623  G4bool onlyIfActive) const
624 {
625  return GetH1InFunction(id, "GetH1", warn, onlyIfActive);
626 }
627 
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
virtual G4bool SetH1YAxisTitle(G4int id, const G4String &title)
virtual G4bool SetH1XAxisTitle(G4int id, const G4String &title)
virtual ~G4H1ToolsManager()
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
virtual G4bool SetH1(G4int id, G4int nbins, G4double xmin, G4double xmax, const G4String &unitName="none", const G4String &fcnName="none", const G4String &binSchemeName="linear")
void ComputeEdges(G4int nbins, G4double xmin, G4double xmax, G4double unit, G4Fcn fcn, G4BinScheme, std::vector< G4double > &edges)
Definition: G4BinScheme.cc:56
G4String name
Definition: TRTMaterials.hh:40
virtual G4double GetH1Xmin(G4int id) const
void AddH1Information(const G4String &name, const G4String &unitName, const G4String &fcnName, G4double unit, G4Fcn fx, G4BinScheme binScheme)
Definition: G4HnManager.cc:57
tools::histo::h1d * GetH1(G4int id, G4bool warn=true, G4bool onlyIfActive=true) const
G4double(* G4Fcn)(G4double)
Definition: G4Fcn.hh:36
virtual G4bool SetH1Title(G4int id, const G4String &title)
int G4int
Definition: G4Types.hh:78
G4HnInformation * GetHnInformation(G4int id, G4String functionName="", G4bool warn=true) const
Definition: G4HnManager.cc:86
G4bool GetActivation(G4int id) const
Definition: G4HnManager.cc:209
const G4AnalysisVerbose * GetVerboseL2() const
void UpdateTitle(G4String &title, const G4String &unitName, const G4String &fcnName)
std::vector< tools::histo::h1d * > fH1Vector
virtual G4String GetH1XAxisTitle(G4int id) const
const G4AnalysisVerbose * GetVerboseL3() const
void AddH1Information(const G4String &name, const G4String &unitName, const G4String &fcnName, G4BinScheme binScheme) const
virtual G4int GetH1Nbins(G4int id) const
const G4AnalysisVerbose * GetVerboseL4() const
bool G4bool
Definition: G4Types.hh:79
virtual G4int GetH1Id(const G4String &name, G4bool warn=true) const
virtual G4bool WriteOnAscii(std::ofstream &output)
virtual tools::histo::h1d * GetH1InFunction(G4int id, G4String functionName, G4bool warn=true, G4bool onlyIfActive=true) const
G4double GetUnitValue(const G4String &unit)
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")
std::map< G4String, G4int > fH1NameIdMap
G4BinScheme fXBinScheme
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4HnManager * fHnManager
virtual G4double GetH1Width(G4int id) const
G4BinScheme fYBinScheme
G4Fcn GetFunction(const G4String &fcnName)
Definition: G4Fcn.cc:36
virtual G4String GetH1YAxisTitle(G4int id) const
G4H1ToolsManager(const G4AnalysisManagerState &state)
G4BinScheme GetBinScheme(const G4String &binSchemeName)
Definition: G4BinScheme.cc:36
#define G4endl
Definition: G4ios.hh:61
G4BinScheme
Definition: G4BinScheme.hh:40
void AddH1Vector(const std::vector< tools::histo::h1d * > &h1Vector)
virtual G4double GetH1Xmax(G4int id) const
G4int RegisterToolsH1(tools::histo::h1d *h1d, const G4String &name)
double G4double
Definition: G4Types.hh:76
virtual G4bool ScaleH1(G4int id, G4double factor)
virtual G4String GetH1Title(G4int id) const
virtual G4bool FillH1(G4int id, G4double value, G4double weight=1.0)
const G4AnalysisVerbose * GetVerboseL1() const
void SetActivation(G4bool activation)
Definition: G4HnManager.cc:140
G4bool IsEmpty() const
const G4AnalysisManagerState & fState