Geant4  10.00.p02
ExG4HbookH1Manager.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$
27 //
30 
31 // Author: Ivana Hrivnacova, 15/06/2011 (ivana@ipno.in2p3.fr)
32 
33 #ifdef G4_USE_HBOOK
34 
35 #include "ExG4HbookH1Manager.hh"
36 #include "ExG4HbookFileManager.hh"
37 #include "G4HnManager.hh"
39 #include "G4AnalysisUtilities.hh"
40 
41 #include <fstream>
42 
43 using namespace G4Analysis;
44 
45 //_____________________________________________________________________________
46 ExG4HbookH1Manager::ExG4HbookH1Manager(const G4AnalysisManagerState& state)
47  : G4VH1Manager(state),
48  fFileManager(0),
49  fH1HbookIdOffset(-1),
50  fH1Vector(),
51  fH1BookingVector(),
52  fH1NameIdMap()
53 {
54 }
55 
56 //_____________________________________________________________________________
57 ExG4HbookH1Manager::~ExG4HbookH1Manager()
58 {
59  // Delete h1
60  Reset();
61 
62  // Delete h1 booking
63  std::vector<h1_booking*>::iterator it;
64  for ( it = fH1BookingVector.begin(); it != fH1BookingVector.end(); it++ ) {
65  delete *it;
66  }
67 }
68 
69 //
70 // utility functions
71 //
72 
73 namespace {
74 
75 //_____________________________________________________________________________
76 void ConvertToFloat(const std::vector<G4double>& doubleVector,
77  std::vector<float>& floatVector)
78 {
79  for (G4int i=0; i<G4int(doubleVector.size()); ++i)
80  floatVector.push_back((float)doubleVector[i]);
81 }
82 
83 //_____________________________________________________________________________
84 void UpdateH1Information(G4HnInformation* information,
85  const G4String& unitName,
86  const G4String& fcnName,
87  G4BinScheme binScheme)
88 {
89  G4double unit = GetUnitValue(unitName);
90  G4Fcn fcn = GetFunction(fcnName);
91  information->fXUnitName = unitName;
92  information->fYUnitName = unitName;
93  information->fXFcnName = fcnName;
94  information->fYFcnName = fcnName;
95  information->fXUnit = unit;
96  information->fYUnit = unit;
97  information->fXFcn = fcn;
98  information->fYFcn = fcn;
99  information->fXBinScheme = binScheme;
100  information->fYBinScheme = binScheme;
101 }
102 
103 //_____________________________________________________________________________
104 h1_booking* CreateH1Booking(const G4String& title,
105  G4int nbins, G4double xmin, G4double xmax,
106  const G4String& unitName,
107  const G4String& fcnName,
108  G4BinScheme binScheme)
109 {
110  G4Fcn fcn = GetFunction(fcnName);
111 
112  h1_booking* h1Booking = 0;
113  if ( binScheme != kLogBinScheme ) {
114  if ( binScheme == kUserBinScheme ) {
115  // This should never happen, but let's make sure about it
116  // by issuing a warning
117  G4ExceptionDescription description;
118  description
119  << " User binning scheme setting was ignored." << G4endl
120  << " Linear binning will be applied with given (nbins, xmin, xmax) values";
121  G4Exception("ExG4HbookH1Manager::CreateH1",
122  "Analysis_W013", JustWarning, description);
123  }
124  h1Booking = new h1_booking(nbins, fcn(xmin), fcn(xmax));
125  // h1_booking object is deleted in destructor
126  }
127  else {
128  // Compute edges
129  G4cout << " 1x1" << G4endl;
130  std::vector<G4double> edges;
131  G4cout << " 1x2" << G4endl;
132  ComputeEdges(nbins, xmin, xmax, fcn, binScheme, edges);
133  G4cout << " 1x2" << G4endl;
134  h1Booking = new h1_booking(edges);
135  // h1_booking object is deleted in destructor
136  }
137 
138  h1Booking->fTitle = title;
139  UpdateTitle(h1Booking->fTitle, unitName, fcnName);
140 
141  return h1Booking;
142 }
143 
144 //_____________________________________________________________________________
145 h1_booking* CreateH1Booking(const G4String& title,
146  const std::vector<G4double>& edges,
147  const G4String& unitName,
148  const G4String& fcnName)
149 {
150  G4Fcn fcn = GetFunction(fcnName);
151 
152  // Apply function
153  std::vector<G4double> newEdges;
154  ComputeEdges(edges, fcn, newEdges);
155 
156  G4cout << " 11" << G4endl;
157  h1_booking* h1Booking = new h1_booking(newEdges);
158  G4cout << " 12" << G4endl;
159  // h1_booking object is deleted in destructor
160 
161  h1Booking->fTitle = title;
162  UpdateTitle(h1Booking->fTitle, unitName, fcnName);
163 
164  return h1Booking;
165 }
166 
167 //_____________________________________________________________________________
168 void UpdateH1Booking(h1_booking* h1Booking,
169  G4int nbins, G4double xmin, G4double xmax,
170  const G4String& unitName,
171  const G4String& fcnName,
172  const G4String& binSchemeName)
173 {
174  G4Fcn fcn = GetFunction(fcnName);
175  G4BinScheme binScheme = GetBinScheme(binSchemeName);
176 
177  if ( binScheme != kLogBinScheme ) {
178  if ( binScheme == kUserBinScheme ) {
179  // This should never happen, but let's make sure about it
180  // by issuing a warning
181  G4ExceptionDescription description;
182  description
183  << " User binning scheme setting was ignored." << G4endl
184  << " Linear binning will be applied with given (nbins, xmin, xmax) values";
185  G4Exception("ExG4HbookH1Manager::SetH1",
186  "Analysis_W013", JustWarning, description);
187  }
188  h1Booking->fNbins = nbins;
189  h1Booking->fXmin = fcn(xmin);
190  h1Booking->fXmax = fcn(xmax);
191  }
192  else {
193  // Compute edges
194  ComputeEdges(nbins, xmin, xmax, fcn, binScheme, h1Booking->fEdges);
195  }
196 
197  UpdateTitle(h1Booking->fTitle, unitName, fcnName);
198 }
199 
200 //_____________________________________________________________________________
201 void UpdateH1Booking(h1_booking* h1Booking,
202  const std::vector<G4double>& edges,
203  const G4String& unitName,
204  const G4String& fcnName)
205 {
206  G4Fcn fcn = GetFunction(fcnName);
207 
208  // Apply function
209  ComputeEdges(edges, fcn, h1Booking->fEdges);
210 
211  UpdateTitle(h1Booking->fTitle, unitName, fcnName);
212 }
213 
214 //_____________________________________________________________________________
215 void ConfigureHbookH1(tools::hbook::h1* h1,
216  G4int nbins, G4double xmin, G4double xmax,
217  const G4String& fcnName,
218  G4BinScheme binScheme)
219 {
220  G4Fcn fcn = GetFunction(fcnName);
221 
222  if ( binScheme != kLogBinScheme ) {
223  if ( binScheme == kUserBinScheme ) {
224  // This should never happen, but let's make sure about it
225  // by issuing a warning
226  G4ExceptionDescription description;
227  description
228  << " User binning scheme setting was ignored." << G4endl
229  << " Linear binning will be applied with given (nbins, xmin, xmax) values";
230  G4Exception("ExG4HbookH1Manager::SetH1",
231  "Analysis_W013", JustWarning, description);
232  }
233  h1->configure(nbins, fcn(xmin), fcn(xmax));
234  }
235  else {
236  // Compute bins
237  std::vector<G4double> edges;
238  ComputeEdges(nbins, xmin, xmax, fcn, binScheme, edges);
239  // Convert to float
240  std::vector<float> fedges;
241  ConvertToFloat(edges, fedges);
242 
243  h1->configure(fedges);
244  }
245 }
246 
247 //_____________________________________________________________________________
248 void ConfigureHbookH1(tools::hbook::h1* h1,
249  const std::vector<G4double>& edges,
250  const G4String& fcnName)
251 {
252  // Apply function to edges
253  G4Fcn fcn = GetFunction(fcnName);
254  std::vector<G4double> newEdges;
255  ComputeEdges(edges, fcn, newEdges);
256 
257  // Convert to float
258  std::vector<float> newFEdges;
259  ConvertToFloat(newEdges, newFEdges);
260 
261  h1->configure(newFEdges);
262 }
263 
264 }
265 
266 //
267 // private methods
268 //
269 
270 //_____________________________________________________________________________
271 void ExG4HbookH1Manager::SetH1HbookIdOffset()
272 {
273 // Set fH1HbookIdOffset if needed
274 
275  if ( fH1HbookIdOffset == -1 ) {
276  if ( fFirstId > 0 )
277  fH1HbookIdOffset = 0;
278  else
279  fH1HbookIdOffset = 1;
280 
281  if ( fH1HbookIdOffset > 0 ) {
282  G4ExceptionDescription description;
283  description << "H1 will be defined in HBOOK with ID = G4_firstHistoId + 1";
284  G4Exception("ExG4HbookH1Manager::SetH1HbookIdOffset()",
285  "Analysis_W011", JustWarning, description);
286  }
287  }
288 }
289 
290 //_____________________________________________________________________________
291 void ExG4HbookH1Manager::AddH1Information(const G4String& name,
292  const G4String& unitName,
293  const G4String& fcnName,
294  G4BinScheme binScheme) const
295 {
296  G4double unit = GetUnitValue(unitName);
297  G4Fcn fcn = GetFunction(fcnName);
298  fHnManager->AddH1Information(name, unitName, fcnName, unit, fcn, binScheme);
299 }
300 
301 //_____________________________________________________________________________
302 G4int ExG4HbookH1Manager::CreateH1FromBooking(h1_booking* h1Booking,
303  G4bool chDir)
304 {
305 // Create h1 from h1_booking.
306 
307  if ( chDir ) {
308  // Go to histograms directory if defined
309  if ( fFileManager->GetHistoDirectoryName() != "" ) {
310  G4String histoPath = "//PAWC/LUN1/";
311  histoPath.append(fFileManager->GetHistoDirectoryName().data());
312  tools::hbook::CHCDIR(histoPath.data()," ");
313  }
314  }
315 
316  G4int index = fH1Vector.size();
317  G4int id = index + fFirstId;
319  info = fHnManager->GetHnInformation(id, "CreateH1FromBooking");
320  // Hbook index
321  G4int hbookIndex = fH1HbookIdOffset + index + fFirstId;
322 
323 #ifdef G4VERBOSE
324  if ( fState.GetVerboseL4() )
325  fState.GetVerboseL4()->Message("create from booking", "h1", info->fName);
326 #endif
327 
328  // Create h1
329  tools::hbook::h1* h1 = 0;
330  if ( ! h1Booking->fEdges.size() ) {
331  h1 = new tools::hbook::h1(
332  hbookIndex, h1Booking->fTitle,
333  h1Booking->fNbins, h1Booking->fXmin, h1Booking->fXmax);
334  }
335  else {
336  // Convert to float
337  std::vector<float> newEdges;
338  ConvertToFloat(h1Booking->fEdges, newEdges);
339 
340  h1 = new tools::hbook::h1(hbookIndex, h1Booking->fTitle, newEdges);
341  }
342 
343  fH1Vector.push_back(h1);
344 
345  if ( chDir ) {
346  if ( fFileManager->GetHistoDirectoryName() != "" ) {
347  // Return to //PAWC/LUN1 :
348  tools::hbook::CHCDIR("//PAWC/LUN1"," ");
349  }
350  }
351 
352 #ifdef G4VERBOSE
353  if ( fState.GetVerboseL3() ) {
354  G4ExceptionDescription description;
355  description << " name : " << info->fName << " hbook index : " << hbookIndex;
356  fState.GetVerboseL3()->Message("create from booking", "h1", description);
357  }
358 #endif
359 
360  return id;
361 }
362 
363 //_____________________________________________________________________________
364 G4int ExG4HbookH1Manager::RegisterH1Booking(const G4String& name,
365  h1_booking* h1Booking)
366 {
367  // Register h1
368  G4int index = fH1BookingVector.size();
369  fH1BookingVector.push_back(h1Booking);
370  fH1NameIdMap[name] = index + fFirstId;
371 
372  // Lock id
373  fLockFirstId = true;
374 
375  return index + fFirstId;
376 }
377 
378 //_____________________________________________________________________________
379 void ExG4HbookH1Manager::BeginCreateH1(const G4String& name)
380 {
381 #ifdef G4VERBOSE
382  if ( fState.GetVerboseL4() )
383  fState.GetVerboseL4()->Message("create", "H1", name);
384 #endif
385 
386  // Set fH1HbookIdOffset if needed
387  SetH1HbookIdOffset();
388 }
389 
390 //_____________________________________________________________________________
391 G4int ExG4HbookH1Manager::FinishCreateH1(
392  const G4String& name, h1_booking* h1Booking,
393  const G4String& unitName, const G4String& fcnName,
394  G4BinScheme binScheme)
395 {
396  // Register h1 booking
397  G4int id = RegisterH1Booking(name, h1Booking);
398 
399  // Save H1 information
400  AddH1Information(name, unitName, fcnName, binScheme);
401 
402  // Create h1 if the file is open
403  if ( fFileManager->IsFile() ) {
404  CreateH1FromBooking(h1Booking);
405  }
406 
407 #ifdef G4VERBOSE
408  if ( fState.GetVerboseL2() ) {
409  G4int hbookIndex = fH1HbookIdOffset + id;
410  G4ExceptionDescription description;
411  description << " name : " << name << " hbook index : " << hbookIndex;
412  fState.GetVerboseL2()->Message("create", "H1", description);
413  }
414 #endif
415 
416  return id;
417 }
418 
419 //_____________________________________________________________________________
420 G4bool ExG4HbookH1Manager::BeginSetH1(
421  G4int id,
422  h1_booking* h1Booking,
423  G4HnInformation* info)
424 {
425  h1Booking = GetH1Booking(id, false);
426  if ( ! h1Booking ) {
427  G4ExceptionDescription description;
428  description << " " << "histogram " << id << " does not exist.";
429  G4Exception("G4HbookAnalysisManager::SetH1()",
430  "Analysis_W007", JustWarning, description);
431  return false;
432  }
433 
434  info = fHnManager->GetHnInformation(id,"SetH1");
435 #ifdef G4VERBOSE
436  if ( fState.GetVerboseL4() )
437  fState.GetVerboseL4()->Message("configure", "H1", info->fName);
438 #endif
439 
440  return true;
441 }
442 
443 //_____________________________________________________________________________
444 G4bool ExG4HbookH1Manager::FinishSetH1(
445  G4int id,
446  G4HnInformation* info,
447  const G4String& unitName,
448  const G4String& fcnName,
449  G4BinScheme binScheme)
450 {
451  // Update information
452  UpdateH1Information(info, unitName, fcnName, binScheme);
453 
454  // Set activation
455  fHnManager->SetActivation(id, true);
456 
457  return true;
458 }
459 
460 
461 //_____________________________________________________________________________
462 void ExG4HbookH1Manager::CreateH1sFromBooking()
463 {
464 // Create all h1 from h1_booking.
465 
466  // Do nothing if any h1 histogram already exists
467  // or no h1 histograms are booked
468  if ( fH1Vector.size() || ( fH1BookingVector.size() == 0 ) ) return;
469 
470  // Go to histograms directory if defined
471  if ( fFileManager->GetHistoDirectoryName() != "" ) {
472  G4String histoPath = "//PAWC/LUN1/";
473  histoPath.append(fFileManager->GetHistoDirectoryName().data());
474  tools::hbook::CHCDIR(histoPath.data()," ");
475  }
476 
477  // Create histograms
478  std::vector<h1_booking*>::const_iterator it;
479  for ( it = fH1BookingVector.begin(); it != fH1BookingVector.end(); ++it) {
480  CreateH1FromBooking(*it, false);
481  }
482 
483  // Return backi from histograms directory if defined
484  if ( fFileManager->GetHistoDirectoryName() != "" ) {
485  // Return to //PAWC/LUN1 :
486  tools::hbook::CHCDIR("//PAWC/LUN1"," ");
487  }
488 }
489 
490 //_____________________________________________________________________________
491 void ExG4HbookH1Manager::Reset()
492 {
493 // Reset histograms and ntuple
494 
495  // Delete histograms
496  std::vector<tools::hbook::h1*>::iterator it;
497  for (it = fH1Vector.begin(); it != fH1Vector.end(); it++ ) {
498  delete *it;
499  }
500 
501  // Clear vectors
502  fH1Vector.clear();
503 }
504 
505 //_____________________________________________________________________________
506 h1_booking* ExG4HbookH1Manager::GetH1Booking(G4int id, G4bool warn) const
507 {
508  G4int index = id - fFirstId;
509  if ( index < 0 || index >= G4int(fH1BookingVector.size()) ) {
510  if ( warn) {
511  G4ExceptionDescription description;
512  description << " " << "histo " << id << " does not exist.";
513  G4Exception("G4HbookAnalysisManager::GetH1Booking()",
514  "Analysis_W007", JustWarning, description);
515  }
516  return 0;
517  }
518 
519  return fH1BookingVector[index];
520 }
521 
522 //
523 // protected methods
524 //
525 
526 //_____________________________________________________________________________
527 G4bool ExG4HbookH1Manager::WriteOnAscii(std::ofstream& output)
528 {
529 // Write selected objects on ASCII file
530 // (Only H1 implemented by now)
531 // According to the implementation by Michel Maire, originally in
532 // extended examples.
533 
534  // h1 histograms
535  for ( G4int i=0; i<G4int(fH1Vector.size()); ++i ) {
536  G4int id = i + fFirstId;
537  G4HnInformation* info
538  = fHnManager->GetHnInformation(id, "WriteOnAscii");
539  // skip writing if activation is enabled and H1 is inactivated
540  if ( ! info->fAscii ) continue;
541  tools::hbook::h1* h1 = fH1Vector[i];
542 
543 #ifdef G4VERBOSE
544  if ( fState.GetVerboseL3() )
545  fState.GetVerboseL3()->Message("write on ascii", "h1", info->fName);
546 #endif
547 
548  output << "\n 1D histogram " << id << ": " << h1->title()
549  << "\n \n \t X \t\t Y" << G4endl;
550 
551  for (G4int j=0; j< G4int(h1->axis().bins()); ++j) {
552  output << " " << j << "\t"
553  << h1->axis().bin_center(j) << "\t"
554  << h1->bin_height(j) << G4endl;
555  }
556  }
557 
558  return true;
559 }
560 
561 //_____________________________________________________________________________
562 tools::hbook::h1* ExG4HbookH1Manager::GetH1InFunction(G4int id,
563  G4String functionName, G4bool warn,
564  G4bool onlyIfActive) const
565 {
566  G4int index = id - fFirstId;
567  if ( index < 0 || index >= G4int(fH1Vector.size()) ) {
568  if ( warn) {
569  G4String inFunction = "ExG4HbookH1Manager::";
570  inFunction += functionName;
571  G4ExceptionDescription description;
572  description << " " << "histogram " << id << " does not exist.";
573  G4Exception(inFunction, "Analysis_W007", JustWarning, description);
574  }
575  return 0;
576  }
577 
578  // Do not return histogram if inactive
579  if ( fState.GetIsActivation() && onlyIfActive && ( ! fHnManager->GetActivation(id) ) ) {
580  return 0;
581  }
582 
583  return fH1Vector[index];
584 }
585 
586 //
587 // public methods
588 //
589 
590 //_____________________________________________________________________________
591 G4int ExG4HbookH1Manager::CreateH1(
592  const G4String& name, const G4String& title,
593  G4int nbins, G4double xmin, G4double xmax,
594  const G4String& unitName, const G4String& fcnName,
595  const G4String& binSchemeName)
596 {
597  BeginCreateH1(name);
598 
599  G4BinScheme binScheme = GetBinScheme(binSchemeName);
600 
601  // Create h1 booking
602  h1_booking* h1Booking
603  = CreateH1Booking(title, nbins, xmin, xmax, unitName, fcnName, binScheme);
604 
605  return FinishCreateH1(name, h1Booking, unitName, fcnName, binScheme);
606 }
607 
608 //_____________________________________________________________________________
609 G4int ExG4HbookH1Manager::CreateH1(
610  const G4String& name, const G4String& title,
611  const std::vector<G4double>& edges,
612  const G4String& unitName,
613  const G4String& fcnName)
614 {
615  BeginCreateH1(name);
616 
617  // Create h1 booking
618  h1_booking* h1Booking
619  = CreateH1Booking(title, edges, unitName, fcnName);
620 
621  return FinishCreateH1(name, h1Booking, unitName, fcnName, kUserBinScheme);
622 }
623 
624 
625 //_____________________________________________________________________________
626 G4bool ExG4HbookH1Manager::SetH1(G4int id,
627  G4int nbins, G4double xmin, G4double xmax,
628  const G4String& unitName,
629  const G4String& fcnName,
630  const G4String& binSchemeName)
631 {
632  h1_booking* h1Booking = 0;
633  G4HnInformation* info = 0;
634 
635  if ( ! BeginSetH1(id, h1Booking, info) ) return false;
636 
637  G4BinScheme binScheme = GetBinScheme(binSchemeName);
638 
639  // Update H1 booking
640  UpdateH1Booking(h1Booking,
641  nbins, xmin, xmax, unitName, fcnName, binScheme);
642 
643  // Re-configure histogram if it was already defined
644  if ( fH1Vector.size() ) {
645  tools::hbook::h1* h1 = GetH1(id);
646  ConfigureHbookH1(h1, nbins, xmin, xmax, fcnName, binScheme);
647  }
648 
649  return FinishSetH1(id, info, unitName, fcnName, binScheme);
650 }
651 
652 //_____________________________________________________________________________
653 G4bool ExG4HbookH1Manager::SetH1(G4int id,
654  const std::vector<G4double>& edges,
655  const G4String& unitName,
656  const G4String& fcnName)
657 {
658  h1_booking* h1Booking = 0;
659  G4HnInformation* info = 0;
660 
661  if ( ! BeginSetH1(id, h1Booking, info) ) return false;
662 
663  // Update H1 booking
664  UpdateH1Booking(h1Booking, edges, unitName, fcnName);
665 
666  // Re-configure histogram if it was already defined
667  if ( fH1Vector.size() ) {
668  tools::hbook::h1* h1 = GetH1(id);
669  ConfigureHbookH1(h1, edges, fcnName);
670  }
671 
672  return
673  FinishSetH1(id, info, unitName, fcnName, kUserBinScheme);
674 }
675 
676 //_____________________________________________________________________________
677 G4bool ExG4HbookH1Manager::ScaleH1(G4int id, G4double factor)
678 {
679  tools::hbook::h1* h1 = GetH1InFunction(id, "ScaleH1", false, false);
680  if ( ! h1 ) return false;
681 
682  return h1->scale(factor);
683 }
684 
685 //_____________________________________________________________________________
686 G4bool ExG4HbookH1Manager::FillH1(G4int id, G4double value, G4double weight)
687 {
688  tools::hbook::h1* h1 = GetH1InFunction(id, "FillH1", true, false);
689  if ( ! h1 ) return false;
690 
691  if ( fState.GetIsActivation() && ( ! fHnManager->GetActivation(id) ) ) {
692  //G4cout << "Skipping FillH1 for " << id << G4endl;
693  return false;
694  }
695 
696  G4HnInformation* info = fHnManager->GetHnInformation(id, "FillH1");
697  h1->fill(info->fXFcn(value/info->fXUnit), weight);
698 #ifdef G4VERBOSE
699  if ( fState.GetVerboseL4() ) {
700  G4ExceptionDescription description;
701  description << " id " << id << " value " << value;
702  fState.GetVerboseL4()->Message("fill", "H1", description);
703  }
704 #endif
705  return true;
706 }
707 
708 //_____________________________________________________________________________
709 tools::hbook::h1* ExG4HbookH1Manager::GetH1(G4int id, G4bool warn,
710  G4bool onlyIfActive) const
711 {
712  return GetH1InFunction(id, "GetH1", warn, onlyIfActive);
713 }
714 
715 //_____________________________________________________________________________
716 G4int ExG4HbookH1Manager::GetH1Id(const G4String& name, G4bool warn) const
717 {
718  std::map<G4String, G4int>::const_iterator it = fH1NameIdMap.find(name);
719  if ( it == fH1NameIdMap.end() ) {
720  if ( warn) {
721  G4String inFunction = "ExG4HbookH1Manager::GetH1Id";
722  G4ExceptionDescription description;
723  description << " " << "histogram " << name << " does not exist.";
724  G4Exception(inFunction, "Analysis_W007", JustWarning, description);
725  }
726  return -1;
727  }
728  return it->second;
729 }
730 
731 //_____________________________________________________________________________
732 G4int ExG4HbookH1Manager::GetH1Nbins(G4int id) const
733 {
734  tools::hbook::h1* h1 = GetH1InFunction(id, "GetH1Nbins");
735  if ( ! h1 ) return 0;
736 
737  return h1->axis().bins();
738 }
739 
740 //_____________________________________________________________________________
741 G4double ExG4HbookH1Manager::GetH1Xmin(G4int id) const
742 {
743 // Returns xmin value with applied unit and histogram function
744 
745  tools::hbook::h1* h1 = GetH1InFunction(id, "GetH1Xmin");
746  if ( ! h1 ) return 0;
747 
748  G4HnInformation* info = fHnManager->GetHnInformation(id, "GetH1Xmin");
749  return info->fXFcn(h1->axis().lower_edge()*info->fXUnit);
750 }
751 
752 //_____________________________________________________________________________
753 G4double ExG4HbookH1Manager::GetH1Xmax(G4int id) const
754 {
755  tools::hbook::h1* h1 = GetH1InFunction(id, "GetH1Xmax");
756  if ( ! h1 ) return 0;
757 
758  G4HnInformation* info = fHnManager->GetHnInformation(id, "GetH1Xmax");
759  return info->fXFcn(h1->axis().upper_edge()*info->fXUnit);
760 }
761 
762 //_____________________________________________________________________________
763 G4double ExG4HbookH1Manager::GetH1Width(G4int id) const
764 {
765  tools::hbook::h1* h1 = GetH1InFunction(id, "GetH1XWidth", true, false);
766  if ( ! h1 ) return 0;
767 
768  G4int nbins = h1->axis().bins();
769  if ( ! nbins ) {
770  G4ExceptionDescription description;
771  description << " nbins = 0 (for h1 id = " << id << ").";
772  G4Exception("ExG4HbookH1Manager::GetH1Width",
773  "Analysis_W014", JustWarning, description);
774  return 0;
775  }
776 
777  G4HnInformation* info = fHnManager->GetHnInformation(id, "GetH1XWidth");
778  return ( info->fXFcn(h1->axis().upper_edge())
779  - info->fXFcn(h1->axis().lower_edge()))*info->fXUnit/nbins;
780 }
781 
782 //_____________________________________________________________________________
783 G4bool ExG4HbookH1Manager::SetH1Title(G4int id, const G4String& title)
784 {
785  h1_booking* h1Booking = GetH1Booking(id, false);
786  if ( ! h1Booking ) {
787  G4ExceptionDescription description;
788  description << " " << "histogram " << id << " does not exist.";
789  G4Exception("G4HbookAnalysisManager::SetH1Title()",
790  "Analysis_W007", JustWarning, description);
791  return false;
792  }
793 
794  h1Booking->fTitle = title;
795  return true;
796 }
797 
798 //_____________________________________________________________________________
799 G4bool ExG4HbookH1Manager::SetH1XAxisTitle(G4int id, const G4String& title)
800 {
801  tools::hbook::h1* h1 = GetH1InFunction(id, "SetH1XAxisTitle");
802  if ( ! h1 ) return false;
803 
804  h1->add_annotation(tools::hbook::key_axis_x_title(), title);
805  return true;
806 }
807 
808 //_____________________________________________________________________________
809 G4bool ExG4HbookH1Manager::SetH1YAxisTitle(G4int id, const G4String& title)
810 {
811  tools::hbook::h1* h1 = GetH1InFunction(id, "SetH1YAxisTitle");
812  if ( ! h1 ) return false;
813 
814  h1->add_annotation(tools::hbook::key_axis_y_title(), title);
815  return true;
816 }
817 
818 //_____________________________________________________________________________
819 G4String ExG4HbookH1Manager::GetH1Title(G4int id) const
820 {
821  h1_booking* h1Booking = GetH1Booking(id, false);
822  if ( ! h1Booking ) {
823  G4ExceptionDescription description;
824  description << " " << "histogram " << id << " does not exist.";
825  G4Exception("G4HbookAnalysisManager::GetH1Title()",
826  "Analysis_W007", JustWarning, description);
827  return "";
828  }
829 
830  return h1Booking->fTitle;
831 }
832 
833 
834 //_____________________________________________________________________________
835 G4String ExG4HbookH1Manager::GetH1XAxisTitle(G4int id) const
836 {
837  tools::hbook::h1* h1 = GetH1InFunction(id, "GetH1XAxisTitle");
838  if ( ! h1 ) return "";
839 
840  G4String title;
841  G4bool result = h1->annotation(tools::hbook::key_axis_x_title(), title);
842  if ( ! result ) {
843  G4ExceptionDescription description;
844  description << " Failed to get x_axis title for h1 id = " << id << ").";
845  G4Exception("ExG4HbookH1Manager::GetH1XAxisTitle",
846  "Analysis_W014", JustWarning, description);
847  return "";
848  }
849 
850  return title;
851 }
852 
853 //_____________________________________________________________________________
854 G4String ExG4HbookH1Manager::GetH1YAxisTitle(G4int id) const
855 {
856  tools::hbook::h1* h1 = GetH1InFunction(id, "GetH1YAxisTitle");
857  if ( ! h1 ) return "";
858 
859  G4String title;
860  G4bool result = h1->annotation(tools::hbook::key_axis_y_title(), title);
861  if ( ! result ) {
862  G4ExceptionDescription description;
863  description << " Failed to get y_axis title for h1 id = " << id << ").";
864  G4Exception("ExG4HbookH1Manager::GetH1YAxisTitle",
865  "Analysis_W014", JustWarning, description);
866  return "";
867  }
868 
869  return title;
870 }
871 
872 //_____________________________________________________________________________
873 G4bool ExG4HbookH1Manager::SetH1HbookIdOffset(G4int offset)
874 {
875  if ( fH1Vector.size() ) {
876  G4ExceptionDescription description;
877  description
878  << "Cannot set H1HbookIdOffset as some H1 histogramms already exist.";
879  G4Exception("G4HbookAnalysisManager::SetH1HbookIdOffset()",
880  "Analysis_W009", JustWarning, description);
881  return false;
882  }
883 
884  if ( fFirstId + offset < 1 ) {
885  G4ExceptionDescription description;
886  description << "The first histogram HBOOK id must be >= 1.";
887  G4Exception("G4HbookAnalysisManager::SetH1HbookIdOffset()",
888  "Analysis_W009", JustWarning, description);
889  return false;
890  }
891 
892  fH1HbookIdOffset = offset;
893  return true;
894 }
895 
896 #endif
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4String name
Definition: TRTMaterials.hh:40
G4double(* G4Fcn)(G4double)
Definition: G4Fcn.hh:36
int G4int
Definition: G4Types.hh:78
void UpdateTitle(G4String &title, const G4String &unitName, const G4String &fcnName)
G4GLOB_DLL std::ostream G4cout
bool G4bool
Definition: G4Types.hh:79
G4double GetUnitValue(const G4String &unit)
Definition of the ExG4HbookH1Manager class.
G4BinScheme fXBinScheme
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
const char * data() const
G4BinScheme fYBinScheme
G4Fcn GetFunction(const G4String &fcnName)
Definition: G4Fcn.cc:36
G4String & append(const G4String &)
G4BinScheme GetBinScheme(const G4String &binSchemeName)
Definition: G4BinScheme.cc:36
#define G4endl
Definition: G4ios.hh:61
void ComputeEdges(G4int nbins, G4double xmin, G4double xmax, G4Fcn fcn, G4BinScheme, std::vector< G4double > &edges)
Definition: G4BinScheme.cc:56
G4BinScheme
Definition: G4BinScheme.hh:40
double G4double
Definition: G4Types.hh:76
Definition of the ExG4HbookFileManager class.