Geant4  10.01.p01
ExG4HbookFileManager.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 "ExG4HbookFileManager.hh"
37 
38 #include <iostream>
39 
40 const G4String ExG4HbookFileManager::fgkDefaultNtupleDirectoryName = "ntuple";
41 
42 //_____________________________________________________________________________
43 ExG4HbookFileManager::ExG4HbookFileManager(const G4AnalysisManagerState& state)
44  : G4VFileManager(state),
45  fFile(0)
46 {
47 }
48 
49 //_____________________________________________________________________________
50 ExG4HbookFileManager::~ExG4HbookFileManager()
51 {
52  delete fFile;
53 }
54 
55 //
56 // public methods
57 //
58 
59 //_____________________________________________________________________________
60 G4bool ExG4HbookFileManager::OpenFile(const G4String& fileName)
61 {
62  // Keep file name
63  fFileName = fileName;
64  G4String name = GetFullFileName();
65 
66 #ifdef G4VERBOSE
67  if ( fState.GetVerboseL4() )
68  fState.GetVerboseL4()->Message("open", "analysis file", name);
69 #endif
70 
71  // delete a previous file if it exists
72  if ( fFile ) delete fFile;
73 
74  tools::hbook::CHCDIR("//PAWC"," ");
75 
76  unsigned int unit = 1;
77  fFile = new tools::hbook::wfile(std::cout, name, unit);
78  if ( ! fFile->is_valid() ) {
79  G4ExceptionDescription description;
80  description << " " << "Cannot open file " << fileName;
81  G4Exception("G4HbookAnalysisManager::OpenFile()",
82  "Analysis_W001", JustWarning, description);
83  return false;
84  }
85 
86  // At this point, in HBOOK, we should have :
87  // - created a //LUN1 directory attached to the file
88  // - created a //PAWC/LUN1 in memory
89  // - be in the directory //PAWC/LUN1.
90 
91  // create an "histo" HBOOK directory both in memory and in the file :
92  if ( fHistoDirectoryName != "" ) {
93  tools::hbook::CHCDIR("//PAWC/LUN1"," ");
94  tools::hbook::CHMDIR(fHistoDirectoryName.data()," ");
95  tools::hbook::CHCDIR("//LUN1"," ");
96  tools::hbook::CHMDIR(fHistoDirectoryName.data()," ");
97  }
98  // the five upper lines could have been done with :
99  //fFile->cd_home();
100  //fFile->mkcd("histo");
101 
102  fLockFileName = true;
103  fLockHistoDirectoryName = true;
104  fLockNtupleDirectoryName = true;
105 
106 #ifdef G4VERBOSE
107  if ( fState.GetVerboseL1() )
108  fState.GetVerboseL1()->Message("open", "analysis file", name);
109 #endif
110 
111  return true;
112 }
113 
114 //_____________________________________________________________________________
115 G4bool ExG4HbookFileManager::WriteFile()
116 {
117 #ifdef G4VERBOSE
118  if ( fState.GetVerboseL4() )
119  fState.GetVerboseL4()->Message("write", "file", GetFullFileName());
120 #endif
121 
122  // Return to //PAWC/LUN1 :
123  //tools::hbook::CHCDIR("//PAWC/LUN1"," ");
124  G4bool result = fFile->write();
125 
126 #ifdef G4VERBOSE
127  if ( fState.GetVerboseL1() )
128  fState.GetVerboseL1()->Message("write", "file", GetFullFileName(), result);
129 #endif
130 
131  return result;
132 }
133 
134 //_____________________________________________________________________________
135 G4bool ExG4HbookFileManager::CloseFile()
136 {
137  // close file
138  G4bool result = fFile->close();
139  fLockFileName = false;
140 
141  return result;
142 }
143 
144 //_____________________________________________________________________________
145 void ExG4HbookFileManager::CreateNtupleDirectory()
146 {
147 // Create an "ntuple" directory both in memory and in the file
148 
149  static G4bool isDone = false;
150 
151  // Do not create directory more than once
152  if (isDone) return;
153 
154  fFile->cd_home(); //go under //PAWC/LUN1
155  if ( fNtupleDirectoryName == "" )
156  fFile->mkcd(fgkDefaultNtupleDirectoryName.data());
157  else
158  fFile->mkcd(fNtupleDirectoryName.data());
159  fLockNtupleDirectoryName = true;
160  isDone = false;
161 }
162 
163 #endif
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4String name
Definition: TRTMaterials.hh:40
bool G4bool
Definition: G4Types.hh:79
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
Definition of the ExG4HbookFileManager class.