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 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  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 
 27 // Author: Ivana Hrivnacova, 15/06/2011  (ivana@ipno.in2p3.fr)
 28 
 29 #include "G4RootFileManager.hh"
 30 #include "G4RootHnFileManager.hh"
 31 #include "G4AnalysisManagerState.hh"
 32 #include "G4AnalysisUtilities.hh"
 33 
 34 #include "tools/wroot/file"
 35 #include "tools/wroot/to"
 36 #include "toolx/zlib"
 37 
 38 using namespace tools;
 39 using namespace G4Analysis;
 40 
 41 //_____________________________________________________________________________
 42 G4RootFileManager::G4RootFileManager(const G4AnalysisManagerState& state)
 43  : G4VTFileManager<G4RootFile>(state)
 44 {
 45   // Create helpers defined in the base class
 46   fH1FileManager = std::make_shared<G4RootHnFileManager<histo::h1d>>(this);
 47   fH2FileManager = std::make_shared<G4RootHnFileManager<histo::h2d>>(this);
 48   fH3FileManager = std::make_shared<G4RootHnFileManager<histo::h3d>>(this);
 49   fP1FileManager = std::make_shared<G4RootHnFileManager<histo::p1d>>(this);
 50   fP2FileManager = std::make_shared<G4RootHnFileManager<histo::p2d>>(this);
 51 }
 52 
 53 //
 54 // private methods
 55 //
 56 
 57 //_____________________________________________________________________________
 58 tools::wroot::directory*  G4RootFileManager::CreateDirectory(
 59   tools::wroot::file* rfile,
 60   const G4String& directoryName,  [[maybe_unused]] const G4String& objectType) const
 61 {
 62   if (rfile == nullptr) return nullptr;
 63 
 64   if ( directoryName == "" ) {
 65     // Do not create a new directory if its name is not set
 66     return &(rfile->dir());
 67   }
 68 
 69   Message(kVL4, "create", "directory for " + objectType, directoryName);
 70 
 71   auto directory = rfile->dir().mkdir(directoryName);
 72   if (directory == nullptr) {
 73     Warn("Cannot create directory " + directoryName, fkClass, "CreateDirectory");
 74     return nullptr;
 75   }
 76   Message(kVL2, "create", "directory for " + objectType, directoryName);
 77 
 78   return directory;
 79 }
 80 
 81 //_____________________________________________________________________________
 82 G4String G4RootFileManager::GetNtupleFileName(
 83                               RootNtupleDescription* ntupleDescription,
 84                               G4bool perThread,
 85                               G4int mainNumber) const
 86 {
 87   // get ntuple file name
 88 
 89   auto ntupleFileName = ntupleDescription->GetFileName();
 90   if (ntupleFileName.size() != 0u) {
 91     if ( perThread ) {
 92       ntupleFileName = GetTnFileName(ntupleFileName, GetFileType());
 93     }
 94   }
 95   else {
 96     // get default file name
 97     ntupleFileName = GetFullFileName(fFileName, perThread);
 98   }
 99 
100   // update filename per mainNumber
101   if ( mainNumber > -1 ) {
102     // update filename per mainNumber
103     ntupleFileName
104       = G4Analysis::GetNtupleFileName(ntupleFileName, GetFileType(), mainNumber);
105   }
106 
107   return ntupleFileName;
108 }
109 
110 //
111 // protected methods
112 //
113 
114 //_____________________________________________________________________________
115 std::shared_ptr<G4RootFile>
116 G4RootFileManager::CreateFileImpl(const G4String& fileName)
117 {
118   // create file
119   std::shared_ptr<wroot::file> file = std::make_shared<wroot::file>(G4cout, fileName);
120   file->add_ziper('Z',toolx::compress_buffer);
121   file->set_compression(GetCompressionLevel());
122 
123   if ( ! file->is_open() ) {
124     Warn("Cannot create file " + fileName, fkClass, "CreateFileImpl");
125     return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr);
126   }
127 
128   // create histo directory
129   tools::wroot::directory* hdirectory
130     = CreateDirectory(file.get(), fHistoDirectoryName, "histograms");
131   if (hdirectory == nullptr) {
132     // Warning is issued in CreateDirectory
133     return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr);
134   }
135 
136   // create ntuple directory
137   tools::wroot::directory* ndirectory
138     = CreateDirectory(file.get(), fNtupleDirectoryName, "ntuples");
139   if (ndirectory == nullptr) {
140     // Warning is issued in CreateDirectory
141     return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr);
142   }
143 
144   return std::make_shared<G4RootFile>(file, hdirectory, ndirectory);
145 }
146 
147 //_____________________________________________________________________________
148 G4bool G4RootFileManager::WriteFileImpl(std::shared_ptr<G4RootFile> file)
149 {
150 // New prototype: called by G4TFileManager base classe
151 
152   // nothing to be done, but file should exist
153   return (file == nullptr) ? false : true;
154 
155 }
156 
157 //_____________________________________________________________________________
158 G4bool G4RootFileManager::CloseFileImpl(std::shared_ptr<G4RootFile> file)
159 {
160 // New prototype: called by G4TFileManager base classe
161 
162   if (file == nullptr) return false;
163 
164   // write file (only once)
165   unsigned int n;
166   std::get<0>(*file)->write(n);
167 
168   // close file
169   std::get<0>(*file)->close();
170 
171   return true;
172 }
173 
174 //
175 // public methods
176 //
177 //_____________________________________________________________________________
178 G4bool G4RootFileManager::OpenFile(const G4String& fileName)
179 {
180 // Open default file
181 
182   // Keep file name
183   fFileName =  fileName;
184   auto name = GetFullFileName();
185 
186   if ( fFile ) {
187     Warn("File " + fileName + " already exists.", fkClass, "OpenFile");
188     fFile.reset();
189   }
190 
191   // Create file (and save in in the file map)
192   fFile = CreateTFile(name);
193   if ( ! fFile ) {
194     Warn("Failed to create file " + fileName, fkClass, "OpenFile");
195     return false;
196   }
197 
198   LockDirectoryNames();
199   fIsOpenFile = true;
200 
201   return true;
202 }
203 
204 //_____________________________________________________________________________
205 std::shared_ptr<G4RootFile> G4RootFileManager::CreateNtupleFile(
206   RootNtupleDescription* ntupleDescription, G4int mainNumber)
207 {
208   // get ntuple file name per object
209   auto perThread = true;
210   auto ntupleFileName = GetNtupleFileName(ntupleDescription, perThread, mainNumber);
211 
212   auto file = GetTFile(ntupleFileName, false);
213   if (! file) {
214     file = CreateTFile(ntupleFileName);
215   }
216 
217   // register file in ntuple description only if it is not main ntuple file
218   // (main ntuple files are managed in main ntuple manager)
219   if ( mainNumber == -1 ) {
220     ntupleDescription->SetFile(file);
221   }
222 
223   return file;
224 }
225 
226 //_____________________________________________________________________________
227 std::shared_ptr<G4RootFile> G4RootFileManager::GetNtupleFile(
228   RootNtupleDescription* ntupleDescription,  G4bool perThread, G4int mainNumber) const
229 {
230   // get ntuple file name per object
231   auto ntupleFileName = GetNtupleFileName(ntupleDescription, perThread, mainNumber);
232 
233   return GetTFile(ntupleFileName, false);
234 }
235 
236 //_____________________________________________________________________________
237 G4bool G4RootFileManager::CloseNtupleFile(
238   RootNtupleDescription* ntupleDescription,  G4int mainNumber)
239 {
240   // auto result = true;
241 
242   // Notify not empty file
243   auto ntupleFileName = GetNtupleFileName(ntupleDescription, true, mainNumber);
244   auto result = SetIsEmpty(ntupleFileName, ! ntupleDescription->GetHasFill());
245 
246   // Ntuple files will be closed with CloseFiles() calls
247   ntupleDescription->GetFile().reset();
248 
249   return result;
250 }
251