Geant4  10.02.p02
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"
40 #include "ExG4HbookP1Manager.hh"
44 #include "G4UnitsTable.hh"
45 
46 #include <iostream>
47 
48 extern "C" int setpawc();
49 extern "C" int setntuc();
50 
51 ExG4HbookAnalysisManager* ExG4HbookAnalysisManager::fgInstance = 0;
52 
53 //_____________________________________________________________________________
54 ExG4HbookAnalysisManager* ExG4HbookAnalysisManager::Create(G4bool /*isMaster*/)
55 {
56  if ( fgInstance == 0 ) {
57  fgInstance = new ExG4HbookAnalysisManager();
58  }
59 
60  return fgInstance;
61 }
62 
63 //_____________________________________________________________________________
64 ExG4HbookAnalysisManager* ExG4HbookAnalysisManager::Instance()
65 {
66  if ( fgInstance == 0 ) {
67  fgInstance = new ExG4HbookAnalysisManager();
68  }
69 
70  return fgInstance;
71 }
72 
73 //_____________________________________________________________________________
74 ExG4HbookAnalysisManager::ExG4HbookAnalysisManager()
75  : G4VAnalysisManager("Hbook", true),
76  fH1Manager(0),
77  fH2Manager(0),
78  fH3Manager(0),
79  fP1Manager(0),
80  fP2Manager(0),
81  fNtupleManager(0),
82  fFileManager(0)
83 {
85  // Hbook output is not supported in MT mode
86  G4ExceptionDescription description;
87  description << " "
88  << "G4HbookAnalysisManager is not supported in multi-threading mode.";
89  G4Exception("ExG4HbookAnalysisManager::ExG4HbookAnalysisManager()",
90  "Analysis_F002", FatalException, description);
91  }
92 
93  if ( fgInstance ) {
94  G4ExceptionDescription description;
95  description << " "
96  << "G4HbookAnalysisManager already exists."
97  << "Cannot create another instance.";
98  G4Exception("ExG4HbookAnalysisManager::ExG4HbookAnalysisManager()",
99  "Analysis_F001", FatalException, description);
100  }
101 
102  fgInstance = this;
103 
104  // Create managers
105  fH1Manager = new ExG4HbookH1Manager(fState);
106  fH2Manager = new ExG4HbookH2Manager(fState);
107  fH3Manager = new ExG4HbookH3DummyManager(fState);
108  fP1Manager = new ExG4HbookP1Manager(fState);
109  fP2Manager = new ExG4HbookP2DummyManager(fState);
110  fNtupleManager = new ExG4HbookNtupleManager(fState);
111  fFileManager = std::make_shared<ExG4HbookFileManager>(fState);
112 
113  // Set managers to base class
114  SetH1Manager(fH1Manager);
115  SetH2Manager(fH2Manager);
116  SetH3Manager(fH3Manager);
117  SetP1Manager(fP1Manager);
118  SetP2Manager(fP2Manager);
119  SetNtupleManager(fNtupleManager);
120  SetFileManager(fFileManager);
121 
122  // Set file manager to component managers
123  fH1Manager->SetFileManager(fFileManager.get());
124  fH2Manager->SetFileManager(fFileManager.get());
125  fP1Manager->SetFileManager(fFileManager.get());
126  fNtupleManager->SetFileManager(fFileManager.get());
127 
128  // Initialize HBOOK :
129  tools::hbook::CHLIMIT(setpawc());
130  setntuc(); //for ntuple.
131 }
132 
133 //_____________________________________________________________________________
134 ExG4HbookAnalysisManager::~ExG4HbookAnalysisManager()
135 {
136  fgInstance = 0;
137 }
138 
139 //
140 // private methods
141 //
142 
143 //_____________________________________________________________________________
144 void ExG4HbookAnalysisManager::Reset()
145 {
146 // Reset histograms and ntuple
147 
148  fH1Manager->Reset();
149  fH2Manager->Reset();
150  fP1Manager->Reset();
151  fNtupleManager->Reset();
152 }
153 
154 //
155 // public methods
156 //
157 
158 //_____________________________________________________________________________
159 G4bool ExG4HbookAnalysisManager::OpenFileImpl(const G4String& fileName)
160 {
161  G4bool finalResult = true;
162  G4bool result = fFileManager->SetFileName(fileName);
163  finalResult = finalResult && result;
164 
165 #ifdef G4VERBOSE
166  G4String name = fFileManager->GetFullFileName();
167  if ( fState.GetVerboseL4() )
168  fState.GetVerboseL4()->Message("open", "analysis file", name);
169 #endif
170 
171  // Open file
172  result = fFileManager->OpenFile(fileName);
173  finalResult = finalResult && result;
174 
175  // Create h1 histrograms
176  fH1Manager->CreateH1sFromBooking();
177 
178  // Create h2 histrograms if any is booked
179  fH2Manager->CreateH2sFromBooking();
180 
181  // Create p1 profiles if any is booked
182  fP1Manager->CreateP1sFromBooking();
183 
184  // Create ntuples if they are booked
185  fNtupleManager->CreateNtuplesFromBooking();
186 
187 #ifdef G4VERBOSE
188  if ( fState.GetVerboseL1() )
189  fState.GetVerboseL1()->Message("open", "analysis file", name);
190 #endif
191 
192  return finalResult;
193 }
194 
195 //_____________________________________________________________________________
196 G4bool ExG4HbookAnalysisManager::WriteImpl()
197 {
198  G4bool finalResult = true;
199 
200  G4bool result = fFileManager->WriteFile();
201  finalResult = finalResult && result;
202 
203  // Write ASCII if activated
204  if ( IsAscii() ) {
205  result = WriteAscii(fFileManager->GetFileName());
206  finalResult = finalResult && result;
207  }
208 
209  return finalResult;
210 }
211 
212 //_____________________________________________________________________________
213 G4bool ExG4HbookAnalysisManager::CloseFileImpl()
214 {
215  G4bool finalResult = true;
216 
217 #ifdef G4VERBOSE
218  if ( fState.GetVerboseL4() )
219  fState.GetVerboseL4()->Message("close", "file", fFileManager->GetFullFileName());
220 #endif
221 
222  // reset data
223  Reset();
224 
225  // close file
226  G4bool result = fFileManager->CloseFile();
227  finalResult = finalResult && result;
228 
229 #ifdef G4VERBOSE
230  if ( fState.GetVerboseL1() )
231  fState.GetVerboseL1()->Message("close", "file", fFileManager->GetFullFileName(), result);
232 #endif
233 
234  return finalResult;
235 }
236 
237 //_____________________________________________________________________________
238 G4bool ExG4HbookAnalysisManager::PlotImpl()
239 {
240  G4ExceptionDescription description;
241  description << " "
242  << "G4HbookAnalysisManager does not support batch plotting.";
243  G4Exception("ExG4HbookAnalysisManager::ExG4HbookAnalysisManager()",
244  "Analysis_W041", JustWarning, description);
245 
246  return false;
247 }
248 
249 //_____________________________________________________________________________
250 G4bool ExG4HbookAnalysisManager::MergeImpl(tools::histo::hmpi* /*hmpi*/)
251 {
252  G4ExceptionDescription description;
253  description << " "
254  << "G4HbookAnalysisManager does not support MPI.";
255  G4Exception("ExG4HbookAnalysisManager::ExG4HbookAnalysisManager()",
256  "Analysis_W041", JustWarning, description);
257 
258  return false;
259 }
260 
261 #endif
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4String name
Definition: TRTMaterials.hh:40
Definition of the ExG4HbookH3DummyManager class.
Definition of the ExG4HbookAnalysisManager class.
Manager class for P2 with dummy implementation.
bool G4bool
Definition: G4Types.hh:79
Definition of the ExG4HbookNtupleManager class.
Manager class for H3 with dummy implementation.
Definition of the ExG4HbookH1Manager class.
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool IsWorkerThread()
Definition: G4Threading.cc:129
Definition of the ExG4HbookH2Manager class.
Definition of the ExG4HbookP2DummyManager class.
Definition of the ExG4HbookFileManager class.
Definition of the ExG4HbookP1Manager class.