Geant4  10.00.p01
ExG4HbookAnalysisManager.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 
36 #include "ExG4HbookFileManager.hh"
37 #include "ExG4HbookH1Manager.hh"
38 #include "ExG4HbookH2Manager.hh"
41 #include "G4UnitsTable.hh"
42 
43 #include <iostream>
44 
45 extern "C" int setpawc();
46 extern "C" int setntuc();
47 
48 ExG4HbookAnalysisManager* ExG4HbookAnalysisManager::fgInstance = 0;
49 
50 //_____________________________________________________________________________
51 ExG4HbookAnalysisManager* ExG4HbookAnalysisManager::Create(G4bool /*isMaster*/)
52 {
53  if ( fgInstance == 0 ) {
54  fgInstance = new ExG4HbookAnalysisManager();
55  }
56 
57  return fgInstance;
58 }
59 
60 //_____________________________________________________________________________
61 ExG4HbookAnalysisManager* ExG4HbookAnalysisManager::Instance()
62 {
63  if ( fgInstance == 0 ) {
64  fgInstance = new ExG4HbookAnalysisManager();
65  }
66 
67  return fgInstance;
68 }
69 
70 //_____________________________________________________________________________
71 ExG4HbookAnalysisManager::ExG4HbookAnalysisManager()
72  : G4VAnalysisManager("Hbook", true),
73  fH1Manager(0),
74  fH2Manager(0),
75  fNtupleManager(0),
76  fFileManager(0)
77 {
79  // Hbook output is not supported in MT mode
80  G4ExceptionDescription description;
81  description << " "
82  << "G4HbookAnalysisManager is not supported in multi-threading mode.";
83  G4Exception("ExG4HbookAnalysisManager::ExG4HbookAnalysisManager()",
84  "Analysis_F002", FatalException, description);
85  }
86 
87  if ( fgInstance ) {
88  G4ExceptionDescription description;
89  description << " "
90  << "G4HbookAnalysisManager already exists."
91  << "Cannot create another instance.";
92  G4Exception("ExG4HbookAnalysisManager::ExG4HbookAnalysisManager()",
93  "Analysis_F001", FatalException, description);
94  }
95 
96  fgInstance = this;
97 
98  // Create managers
99  fH1Manager = new ExG4HbookH1Manager(fState);
100  fH2Manager = new ExG4HbookH2Manager(fState);
101  fNtupleManager = new ExG4HbookNtupleManager(fState);
102  fFileManager = new ExG4HbookFileManager(fState);
103 
104  // Set managers to base class
105  SetH1Manager(fH1Manager);
106  SetH2Manager(fH2Manager);
107  SetNtupleManager(fNtupleManager);
108  SetFileManager(fFileManager);
109 
110  // Set file manager to component managers
111  fH1Manager->SetFileManager(fFileManager);
112  fH2Manager->SetFileManager(fFileManager);
113  fNtupleManager->SetFileManager(fFileManager);
114 
115  // Initialize HBOOK :
116  tools::hbook::CHLIMIT(setpawc());
117  setntuc(); //for ntuple.
118 }
119 
120 //_____________________________________________________________________________
121 ExG4HbookAnalysisManager::~ExG4HbookAnalysisManager()
122 {
123  fgInstance = 0;
124 }
125 
126 //
127 // private methods
128 //
129 
130 //_____________________________________________________________________________
131 void ExG4HbookAnalysisManager::Reset()
132 {
133 // Reset histograms and ntuple
134 
135  fH1Manager->Reset();
136  fH2Manager->Reset();
137  fNtupleManager->Reset();
138 }
139 
140 //
141 // public methods
142 //
143 
144 //_____________________________________________________________________________
145 G4bool ExG4HbookAnalysisManager::OpenFileImpl(const G4String& fileName)
146 {
147  G4bool finalResult = true;
148  G4bool result = fFileManager->SetFileName(fileName);
149  finalResult = finalResult && result;
150 
151 #ifdef G4VERBOSE
152  G4String name = fFileManager->GetFullFileName();
153  if ( fState.GetVerboseL4() )
154  fState.GetVerboseL4()->Message("open", "analysis file", name);
155 #endif
156 
157  // Open file
158  result = fFileManager->OpenFile(fileName);
159  finalResult = finalResult && result;
160 
161  // Create h1 histrograms
162  fH1Manager->CreateH1sFromBooking();
163 
164  // Create h2 histrograms if any is booked
165  fH2Manager->CreateH2sFromBooking();
166 
167  // Create ntuples if they are booked
168  fNtupleManager->CreateNtuplesFromBooking();
169 
170 #ifdef G4VERBOSE
171  if ( fState.GetVerboseL1() )
172  fState.GetVerboseL1()->Message("open", "analysis file", name);
173 #endif
174 
175  return finalResult;
176 }
177 
178 //_____________________________________________________________________________
179 G4bool ExG4HbookAnalysisManager::WriteImpl()
180 {
181  G4bool finalResult = true;
182 
183  G4bool result = fFileManager->WriteFile();
184  finalResult = finalResult && result;
185 
186  // Write ASCII if activated
187  if ( IsAscii() ) {
188  result = WriteAscii(fFileManager->GetFileName());
189  finalResult = finalResult && result;
190  }
191 
192  return finalResult;
193 }
194 
195 //_____________________________________________________________________________
196 G4bool ExG4HbookAnalysisManager::CloseFileImpl()
197 {
198  G4bool finalResult = true;
199 
200 #ifdef G4VERBOSE
201  if ( fState.GetVerboseL4() )
202  fState.GetVerboseL4()->Message("close", "file", fFileManager->GetFullFileName());
203 #endif
204 
205  // reset data
206  Reset();
207 
208  // close file
209  G4bool result = fFileManager->CloseFile();
210  finalResult = finalResult && result;
211 
212 #ifdef G4VERBOSE
213  if ( fState.GetVerboseL1() )
214  fState.GetVerboseL1()->Message("close", "file", fFileManager->GetFullFileName(), result);
215 #endif
216 
217  return finalResult;
218 }
219 
220 #endif
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4String name
Definition: TRTMaterials.hh:40
Definition of the ExG4HbookAnalysisManager class.
bool G4bool
Definition: G4Types.hh:79
Definition of the ExG4HbookNtupleManager class.
Definition of the ExG4HbookH1Manager class.
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
Definition of the ExG4HbookH2Manager class.
Definition of the ExG4HbookFileManager class.