Import Geant4 10.7.0 source tree

This commit is contained in:
Gabriele Cosmo
2020-12-04 12:30:43 +01:00
parent 67ba86d073
commit dab42d2018
3770 changed files with 226369 additions and 286486 deletions
@@ -40,7 +40,7 @@
#include <memory>
class G4XmlFileManager;
class G4XmlNtupleManager;
class G4XmlNtupleFileManager;
class G4XmlAnalysisManager : public G4ToolsAnalysisManager
{
@@ -90,8 +90,8 @@ class G4XmlAnalysisManager : public G4ToolsAnalysisManager
G4bool Reset();
// data members
G4XmlNtupleManager* fNtupleManager;
std::shared_ptr<G4XmlFileManager> fFileManager;
std::shared_ptr<G4XmlNtupleFileManager> fNtupleFileManager;
};
#include "G4XmlAnalysisManager.icc"
@@ -24,6 +24,7 @@
// ********************************************************************
//
#include "G4XmlNtupleFileManager.hh"
#include "G4XmlNtupleManager.hh"
#include "G4XmlFileManager.hh"
@@ -50,7 +51,7 @@ G4bool G4XmlAnalysisManager::WriteT(const std::vector<T*>& htVector,
#endif
G4String path = "/";
path.append(directoryName);
std::shared_ptr<std::ofstream> hnFile = fFileManager->GetHnFile();
std::shared_ptr<std::ofstream> hnFile = fFileManager->GetFile();
// Do nothing if there is no file
if ( ! hnFile.get() ) return false;
@@ -63,7 +64,7 @@ G4bool G4XmlAnalysisManager::WriteT(const std::vector<T*>& htVector,
"Analysis_W022", JustWarning, description);
return false;
}
fFileManager->LockHistoDirectoryName();
fFileManager->LockDirectoryNames();
}
return true;
@@ -80,28 +81,28 @@ G4bool G4XmlAnalysisManager::IsOpenFileImpl() const
inline
tools::waxml::ntuple* G4XmlAnalysisManager::GetNtuple() const
{
return fNtupleManager->GetNtuple();
return fNtupleFileManager->GetNtupleManager()->GetNtuple();
}
//_____________________________________________________________________________
inline
tools::waxml::ntuple* G4XmlAnalysisManager::GetNtuple(G4int ntupleId) const
{
return fNtupleManager->GetNtuple(ntupleId);
return fNtupleFileManager->GetNtupleManager()->GetNtuple(ntupleId);
}
//_____________________________________________________________________________
inline
std::vector<tools::waxml::ntuple*>::iterator G4XmlAnalysisManager::BeginNtuple()
{
return fNtupleManager->BeginNtuple();
return fNtupleFileManager->GetNtupleManager()->BeginNtuple();
}
//_____________________________________________________________________________
inline
std::vector<tools::waxml::ntuple*>::iterator G4XmlAnalysisManager::EndNtuple()
{
return fNtupleManager->EndNtuple();
return fNtupleFileManager->GetNtupleManager()->EndNtuple();
}
//_____________________________________________________________________________
@@ -109,7 +110,7 @@ inline
std::vector<tools::waxml::ntuple*>::const_iterator
G4XmlAnalysisManager::BeginConstNtuple() const
{
return fNtupleManager->BeginConstNtuple();
return fNtupleFileManager->GetNtupleManager()->BeginConstNtuple();
}
//_____________________________________________________________________________
@@ -117,5 +118,5 @@ inline
std::vector<tools::waxml::ntuple*>::const_iterator
G4XmlAnalysisManager::EndConstNtuple() const
{
return fNtupleManager->EndConstNtuple();
return fNtupleFileManager->GetNtupleManager()->EndConstNtuple();
}
+21 -21
View File
@@ -31,8 +31,9 @@
#ifndef G4XmlFileManager_h
#define G4XmlFileManager_h 1
#include "G4VFileManager.hh"
#include "G4VTFileManager.hh"
#include "G4TNtupleDescription.hh"
#include "G4TFileManager.hh"
#include "globals.hh"
#include "tools/waxml/ntuple"
@@ -40,40 +41,39 @@
#include <fstream>
#include <memory>
// Type aliases
using XmlNtupleDescription = G4TNtupleDescription<tools::waxml::ntuple, std::ofstream>;
class G4AnalysisManagerState;
class G4XmlFileManager : public G4VFileManager
class G4XmlFileManager : public G4VTFileManager<std::ofstream>
{
public:
explicit G4XmlFileManager(const G4AnalysisManagerState& state);
~G4XmlFileManager();
// Type aliases
using NtupleType = tools::waxml::ntuple;
using NtupleDescriptionType = G4TNtupleDescription<NtupleType>;
using G4BaseFileManager::GetNtupleFileName;
using G4VTFileManager<std::ofstream>::WriteFile;
using G4VTFileManager<std::ofstream>::CloseFile;
// Methods to manipulate output files
virtual G4bool OpenFile(const G4String& fileName) final;
virtual G4bool WriteFile() final;
virtual G4bool CloseFile() final;
virtual G4String GetFileType() const final { return "xml"; }
// Specific methods for files per objects
G4bool CreateHnFile();
G4bool CloseHnFile();
G4bool CreateNtupleFile(NtupleDescriptionType* ntupleDescription);
G4bool CloseNtupleFile(NtupleDescriptionType* ntupleDescription);
// Get methods
std::shared_ptr<std::ofstream> GetHnFile() const;
G4bool CreateNtupleFile(XmlNtupleDescription* ntupleDescription);
G4bool CloseNtupleFile(XmlNtupleDescription* ntupleDescription);
protected:
// Methods derived from templated base class
virtual std::shared_ptr<std::ofstream> CreateFileImpl(const G4String& fileName) final;
virtual G4bool WriteFileImpl(std::shared_ptr<std::ofstream> file) final;
virtual G4bool CloseFileImpl(std::shared_ptr<std::ofstream> file) final;
private:
// data members
std::shared_ptr<std::ofstream> fHnFile;
// utility method
G4String GetNtupleFileName(XmlNtupleDescription* ntupleDescription);
};
// inline functions
inline std::shared_ptr<std::ofstream> G4XmlFileManager::GetHnFile() const
{ return fHnFile; }
#endif
@@ -0,0 +1,61 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// The manager histogram/profile Xml file output.
// Author: Ivana Hrivnacova, 15/09/2020 (ivana@ipno.in2p3.fr)
#ifndef G4XmlHnFileManager_h
#define G4XmlHnFileManager_h 1
#include "G4VTHnFileManager.hh"
class G4XmlFileManager;
template <typename HT>
class G4XmlHnFileManager : public G4VTHnFileManager<HT>
{
public:
G4XmlHnFileManager(G4XmlFileManager* fileManger)
: G4VTHnFileManager<HT>(), fFileManager(fileManger) {}
virtual ~G4XmlHnFileManager() {}
// Methods for writing objects
// Write to a new file (the file is closed after write)
virtual G4bool WriteExtra(HT* ht, const G4String& htName, const G4String& fileName) final;
// Write to the default file (handled with OpenFile()/CloseFile methods)
virtual G4bool Write(HT* ht, const G4String& htName, G4String& fileName) final;
private:
// Data members
G4XmlFileManager* fFileManager;
};
// inline functions
#include "G4XmlHnFileManager.icc"
#endif
@@ -0,0 +1,92 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// Author: Ivana Hrivnacova, 15/09/2020 (ivana@ipno.in2p3.fr)
#include "tools/waxml/histos"
#include "tools/waxml/begend"
//_____________________________________________________________________________
template <typename HT>
G4bool G4XmlHnFileManager<HT>::WriteExtra(
HT* ht, const G4String& htName, const G4String& fileName)
{
// create a new file
std::ofstream hnFile(fileName);
// Do nothing if there is no file
if ( ! hnFile.is_open() ) return false;
// Write file opening
tools::waxml::begin(hnFile);
// no directory supported in this mode
G4String path = "/";
// write histo
auto result = tools::waxml::write(hnFile, *ht, path, htName);
if ( ! result) {
// TO DO: give an exception about failure
return false;
}
// close file
tools::waxml::end(hnFile);
hnFile.close();
return true;
}
//_____________________________________________________________________________
template <typename HT>
G4bool G4XmlHnFileManager<HT>::Write(
HT* ht, const G4String& htName, G4String& fileName)
{
if ( fileName.empty()) {
// should not happen
G4cerr << "!!! Xml file name not defined." << G4endl;
G4cerr << "!!! Write " << htName << " failed." << G4endl;
return false;
}
auto hnFile = fFileManager->GetTFile(fileName);
if ( hnFile == nullptr ) {
// TO DO: give an exception,
// this prototype can be used only with OpenFile() mode
G4ExceptionDescription description;
description
<< "Failed to get Xml file " << fileName;
G4Exception("G4XmlHnFileManager<HT>::Write()",
"Analysis_W022", JustWarning, description);
return false;
}
G4String path = "/";
path.append(fFileManager->GetHistoDirectoryName());
auto result = tools::waxml::write(*hnFile, *ht, path, htName);
fFileManager->LockDirectoryNames();
return result;
}
@@ -0,0 +1,81 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// Manager class for ntuple Xml file output.
//
// Author: Ivana Hrivnacova, 15/09/2020 (ivana@ipno.in2p3.fr)
#ifndef G4XmlNtupleFileManager_h
#define G4XmlNtupleFileManager_h 1
#include "G4VNtupleFileManager.hh"
#include "globals.hh"
#include "tools/waxml/ntuple"
#include <memory>
class G4XmlFileManager;
class G4XmlNtupleManager;
class G4XmlNtupleFileManager : public G4VNtupleFileManager
{
public:
explicit G4XmlNtupleFileManager(const G4AnalysisManagerState& state);
~G4XmlNtupleFileManager();
virtual std::shared_ptr<G4VNtupleManager> CreateNtupleManager() override;
// Methods to be performed at file management
virtual G4bool ActionAtOpenFile(const G4String& fileName) override;
virtual G4bool ActionAtWrite() override;
virtual G4bool ActionAtCloseFile(G4bool reset) override;
virtual G4bool Reset() override;
void SetFileManager(std::shared_ptr<G4XmlFileManager> fileManager);
std::shared_ptr<G4XmlNtupleManager> GetNtupleManager() const;
private:
// methods
G4bool CloseNtupleFiles();
// data members
std::shared_ptr<G4XmlFileManager> fFileManager;
std::shared_ptr<G4XmlNtupleManager> fNtupleManager;
};
// inline functions
inline void G4XmlNtupleFileManager::SetFileManager(
std::shared_ptr<G4XmlFileManager> fileManager)
{ fFileManager = fileManager; }
inline std::shared_ptr<G4XmlNtupleManager> G4XmlNtupleFileManager::GetNtupleManager() const
{ return fNtupleManager; }
#endif
@@ -39,40 +39,38 @@
#include <vector>
#include <memory>
// Types alias
using XmlNtupleDescription = G4TNtupleDescription<tools::waxml::ntuple, std::ofstream>;
class G4XmlFileManager;
class G4XmlNtupleManager : public G4TNtupleManager<tools::waxml::ntuple>
class G4XmlNtupleManager : public G4TNtupleManager<tools::waxml::ntuple,
std::ofstream>
{
friend class G4XmlAnalysisManager;
friend class G4XmlNtupleFileManager;
public:
explicit G4XmlNtupleManager(const G4AnalysisManagerState& state);
~G4XmlNtupleManager();
private:
// Types alias
using NtupleType = tools::waxml::ntuple;
using NtupleDescriptionType = G4TNtupleDescription<NtupleType>;
// Functions specific to the output type
//
// Set methods
void SetFileManager(std::shared_ptr<G4XmlFileManager> fileManager);
// Access to ntuple vector (needed for Write())
const std::vector<NtupleDescriptionType*>& GetNtupleDescriptionVector() const;
const std::vector<XmlNtupleDescription*>& GetNtupleDescriptionVector() const;
// Methods from the templated base class
//
virtual void CreateTNtuple(
NtupleDescriptionType* ntupleDescription,
const G4String& name, const G4String& title) final;
virtual void CreateTNtupleFromBooking(
NtupleDescriptionType* ntupleDescription) final;
XmlNtupleDescription* ntupleDescription) final;
virtual void FinishTNtuple(
NtupleDescriptionType* ntupleDescription,
XmlNtupleDescription* ntupleDescription,
G4bool fromBooking) final;
// data members
@@ -86,7 +84,7 @@ inline void
G4XmlNtupleManager::SetFileManager(std::shared_ptr<G4XmlFileManager> fileManager)
{ fFileManager = fileManager; }
inline const std::vector<G4TNtupleDescription<tools::waxml::ntuple>*>&
inline const std::vector<G4TNtupleDescription<tools::waxml::ntuple, std::ofstream>*>&
G4XmlNtupleManager::GetNtupleDescriptionVector() const
{ return fNtupleDescriptionVector; }
@@ -46,6 +46,8 @@ class G4XmlRFileManager : public G4BaseFileManager
explicit G4XmlRFileManager(const G4AnalysisManagerState& state);
~G4XmlRFileManager();
virtual G4String GetFileType() const final { return "xml"; }
// Methods to manipulate input files
virtual G4bool OpenRFile(const G4String& fileName);
+4
View File
@@ -14,6 +14,9 @@ geant4_define_module(NAME G4xml
G4XmlAnalysisReader.hh
G4XmlAnalysisReader.icc
G4XmlFileManager.hh
G4XmlHnFileManager.hh
G4XmlHnFileManager.icc
G4XmlNtupleFileManager.hh
G4XmlNtupleManager.hh
G4XmlRFileManager.hh
G4XmlRNtupleManager.hh
@@ -22,6 +25,7 @@ geant4_define_module(NAME G4xml
SOURCES
G4XmlAnalysisManager.cc
G4XmlAnalysisReader.cc
G4XmlNtupleFileManager.cc
G4XmlFileManager.cc
G4XmlNtupleManager.cc
G4XmlRFileManager.cc
+45 -92
View File
@@ -28,11 +28,14 @@
#include "G4XmlAnalysisManager.hh"
#include "G4XmlFileManager.hh"
#include "G4XmlNtupleManager.hh"
#include "G4XmlNtupleFileManager.hh"
#include "G4AnalysisManagerState.hh"
#include "G4AnalysisUtilities.hh"
#include "G4Threading.hh"
#include "G4AutoLock.hh"
using std::make_shared;
// mutex in a file scope
namespace {
@@ -71,8 +74,8 @@ G4bool G4XmlAnalysisManager::IsInstance()
//_____________________________________________________________________________
G4XmlAnalysisManager::G4XmlAnalysisManager(G4bool isMaster)
: G4ToolsAnalysisManager("Xml", isMaster),
fNtupleManager(nullptr),
fFileManager(nullptr)
fFileManager(nullptr),
fNtupleFileManager(nullptr)
{
if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
G4ExceptionDescription description;
@@ -86,15 +89,14 @@ G4XmlAnalysisManager::G4XmlAnalysisManager(G4bool isMaster)
if ( isMaster ) fgMasterInstance = this;
fgInstance = this;
// Create managers
fNtupleManager = new G4XmlNtupleManager(fState);
// File Manager
fFileManager = std::make_shared<G4XmlFileManager>(fState);
fNtupleManager->SetFileManager(fFileManager);
// The managers will be deleted by the base class
// Set managers to base class which takes then their ownership
SetNtupleManager(fNtupleManager);
SetFileManager(fFileManager);
// Ntuple file manager
fNtupleFileManager = std::make_shared<G4XmlNtupleFileManager>(fState);
fNtupleFileManager->SetFileManager(fFileManager);
fNtupleFileManager->SetBookingManager(fNtupleBookingManager);
}
//_____________________________________________________________________________
@@ -233,32 +235,6 @@ G4bool G4XmlAnalysisManager::WriteP2()
return result;
}
//_____________________________________________________________________________
G4bool G4XmlAnalysisManager::WriteNtuple()
{
auto ntupleVector = fNtupleManager->GetNtupleDescriptionVector();
for ( auto ntuple : ntupleVector ) {
if ( ntuple->fNtuple ) ntuple->fNtuple->write_trailer();
}
return true;
}
//_____________________________________________________________________________
G4bool G4XmlAnalysisManager::CloseNtupleFiles()
{
auto ntupleDescriptionVector = fNtupleManager->GetNtupleDescriptionVector();
// Close ntuple files
for ( auto ntupleDescription : ntupleDescriptionVector) {
fFileManager->CloseNtupleFile((ntupleDescription));
}
return true;
}
//_____________________________________________________________________________
G4bool G4XmlAnalysisManager::Reset()
{
@@ -269,12 +245,12 @@ G4bool G4XmlAnalysisManager::Reset()
auto result = G4ToolsAnalysisManager::Reset();
finalResult = finalResult && result;
result = fNtupleManager->Reset(true);
result = fNtupleFileManager->Reset();
finalResult = finalResult && result;
return finalResult;
}
//
// protected methods
//
@@ -282,36 +258,20 @@ G4bool G4XmlAnalysisManager::Reset()
//_____________________________________________________________________________
G4bool G4XmlAnalysisManager::OpenFileImpl(const G4String& fileName)
{
// Create ntuple manager(s)
// and set it to base class which takes then their ownership
SetNtupleManager(fNtupleFileManager->CreateNtupleManager());
auto finalResult = true;
auto result = fFileManager->SetFileName(fileName);
// Save file name in file manager
auto result = fFileManager->OpenFile(fileName);
finalResult = finalResult && result;
#ifdef G4VERBOSE
auto name = fFileManager->GetFullFileName();
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("open", "analysis file", name);
}
#endif
// Only lock file name in file manager
result = fFileManager->OpenFile(fileName);
// Open ntuple files and create ntuples from bookings
result = fNtupleFileManager->ActionAtOpenFile(fFileManager->GetFullFileName());
finalResult = finalResult && result;
// Create histograms file (on master)
if ( fState.GetIsMaster() ) {
result = fFileManager->CreateHnFile();
finalResult = finalResult && result;
}
// Create ntuples if they are booked
// (The files will be created with creating ntuples)
fNtupleManager->CreateNtuplesFromBooking();
#ifdef G4VERBOSE
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()->Message("open", "analysis file", name, finalResult);
#endif
return finalResult;
}
@@ -321,13 +281,13 @@ G4bool G4XmlAnalysisManager::WriteImpl()
auto finalResult = true;
#ifdef G4VERBOSE
auto name = fFileManager->GetFullFileName();
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("write", "files", name);
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("write", "files", "");
}
#endif
// ntuples
WriteNtuple();
fNtupleFileManager->ActionAtWrite();
if ( ! fgMasterInstance &&
( ( ! fH1Manager->IsEmpty() ) || ( ! fH2Manager->IsEmpty() ) ||
@@ -341,10 +301,6 @@ G4bool G4XmlAnalysisManager::WriteImpl()
<< " " << "Histogram data will not be merged.";
G4Exception("G4XmlAnalysisManager::Write()",
"Analysis_W031", JustWarning, description);
// Create Hn file per thread
auto result = fFileManager->CreateHnFile();
if ( ! result ) return false;
}
// H1
@@ -371,12 +327,12 @@ G4bool G4XmlAnalysisManager::WriteImpl()
if ( IsAscii() ) {
result = WriteAscii(fFileManager->GetFileName());
finalResult = finalResult && result;
}
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()
->Message("write", "file", fFileManager->GetFullFileName(), finalResult);
if ( fState.GetVerboseL2() ) {
fState.GetVerboseL2()->Message("write", "files", "", finalResult);
}
#endif
return finalResult;
@@ -392,36 +348,33 @@ G4bool G4XmlAnalysisManager::CloseFileImpl(G4bool reset)
fState.GetVerboseL4()->Message("close", "files", "");
#endif
// Unlock file name only
auto result = fFileManager->CloseFile();
// close open files
auto result = fFileManager->CloseFiles();
finalResult = finalResult && result;
// Close Hn file
result = fFileManager->CloseHnFile();
finalResult = finalResult && result;
// result = fFileManager->CloseHnFile();
// finalResult = finalResult && result;
// Close ntuple files
result = CloseNtupleFiles();
result = fNtupleFileManager->ActionAtCloseFile(reset);
finalResult = finalResult && result;
// reset data
// Reset data
if ( reset ) {
result = Reset();
} else {
// ntuple must be reset
result = fNtupleManager->Reset(true);
if ( ! result ) {
G4ExceptionDescription description;
description << " " << "Resetting data failed";
G4Exception("G4XmlAnalysisManager::CloseFile()",
"Analysis_W021", JustWarning, description);
}
finalResult = finalResult && result;
}
if ( ! result ) {
G4ExceptionDescription description;
description << " " << "Resetting data failed";
G4Exception("G4XmlAnalysisManager::CloseFile()",
"Analysis_W021", JustWarning, description);
}
finalResult = finalResult && result;
// delete files if empty
// (ntuple files are created only if an ntuple is created)
if ( fFileManager->GetHnFile().get() &&
if ( fFileManager->GetFile() &&
fH1Manager->IsEmpty() && fH2Manager->IsEmpty() && fH3Manager->IsEmpty() &&
fP1Manager->IsEmpty() && fP2Manager->IsEmpty() ) {
result = ! std::remove(fFileManager->GetFullFileName());
+143 -113
View File
@@ -27,24 +27,90 @@
// Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
#include "G4XmlFileManager.hh"
#include "G4XmlHnFileManager.hh"
#include "G4AnalysisManagerState.hh"
#include "G4AnalysisUtilities.hh"
#include "tools/waxml/begend"
using namespace G4Analysis;
using namespace tools;
//_____________________________________________________________________________
G4XmlFileManager::G4XmlFileManager(const G4AnalysisManagerState& state)
: G4VFileManager(state),
fHnFile(nullptr)
: G4VTFileManager<std::ofstream>(state)
{
// Create helpers defined in the base class
fH1FileManager = std::make_shared<G4XmlHnFileManager<histo::h1d>>(this);
fH2FileManager = std::make_shared<G4XmlHnFileManager<histo::h2d>>(this);
fH3FileManager = std::make_shared<G4XmlHnFileManager<histo::h3d>>(this);
fP1FileManager = std::make_shared<G4XmlHnFileManager<histo::p1d>>(this);
fP2FileManager = std::make_shared<G4XmlHnFileManager<histo::p2d>>(this);
}
//_____________________________________________________________________________
G4XmlFileManager::~G4XmlFileManager()
{}
//
// private methods
//
//_____________________________________________________________________________
G4String G4XmlFileManager::GetNtupleFileName(XmlNtupleDescription* ntupleDescription)
{
// get ntuple file name
auto ntupleFileName = ntupleDescription->fFileName;
if ( ntupleFileName.size() ) {
// update filename per object per thread
ntupleFileName = GetTnFileName(ntupleFileName, GetFileType());
} else {
// compose ntuple file name from the default file name
ntupleFileName = GetNtupleFileName(ntupleDescription->fNtupleBooking.name());
}
return ntupleFileName;
}
//
// protected methods
//
//_____________________________________________________________________________
std::shared_ptr<std::ofstream> G4XmlFileManager::CreateFileImpl(const G4String& fileName)
{
std::shared_ptr<std::ofstream> file = std::make_shared<std::ofstream>(fileName);
if ( file->fail() ) {
file = nullptr;
G4ExceptionDescription description;
description << " " << "Cannot create file " << fileName;
G4Exception("G4XmlFileManager::CreateFileImpl()",
"Analysis_W001", JustWarning, description);
return nullptr;
}
waxml::begin(*file);
return file;
}
//_____________________________________________________________________________
G4bool G4XmlFileManager::WriteFileImpl(std::shared_ptr<std::ofstream> /*file*/)
{
// Nothing to be done here
return true;
}
//_____________________________________________________________________________
G4bool G4XmlFileManager::CloseFileImpl(std::shared_ptr<std::ofstream> file)
{
if ( ! file ) return false;
// close file
waxml::end(*file);
file->close();
return true;
}
//
// public methods
//
@@ -54,144 +120,108 @@ G4bool G4XmlFileManager::OpenFile(const G4String& fileName)
{
// Keep and locks file name
fFileName = fileName;
fLockFileName = true;
auto name = GetFullFileName(fFileName);
if ( fFile ) {
G4ExceptionDescription description;
description << "File " << fileName << " already exists.";
G4Exception("G4XmlFileManager::OpenFile()",
"Analysis_W001", JustWarning, description);
fFile.reset();
}
// Create histograms file (on master)
if ( fState.GetIsMaster() ) {
// Create file (and save in in the file map (on master only)
fFile = CreateTFile(name);
if ( ! fFile) {
G4ExceptionDescription description;
description << "Failed to create file " << fileName;
G4Exception("G4XmlFileManager::OpenFile()",
"Analysis_W001", JustWarning, description);
return false;
}
}
fIsOpenFile = true;
return true;
}
//_____________________________________________________________________________
G4bool G4XmlFileManager::WriteFile()
{
// Nothing to be done here
return true;
}
//_____________________________________________________________________________
G4bool G4XmlFileManager::CloseFile()
{
// Unlock file name
fLockFileName = false;
fIsOpenFile = false;
return true;
}
//_____________________________________________________________________________
G4bool G4XmlFileManager::CreateHnFile()
{
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("create", "histo file", GetFullFileName());
#endif
// delete a previous file if it exists
//if ( fHnFile ) delete fHnFile;
fHnFile = std::make_shared<std::ofstream>(GetFullFileName());
if ( fHnFile->fail() ) {
fHnFile = nullptr;
G4ExceptionDescription description;
description << " " << "Cannot open file " << GetFullFileName();
G4Exception("G4XmlFileManager::CreateHnFile()",
"Analysis_W001", JustWarning, description);
return false;
}
tools::waxml::begin(*fHnFile);
#ifdef G4VERBOSE
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()->Message("create", "histo file", GetFullFileName());
#endif
return true;
}
//_____________________________________________________________________________
G4bool G4XmlFileManager::CloseHnFile()
{
// No file may be open if no master manager is instantiated
// and no histograms were booked
if ( ! fHnFile.get() ) return true;
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("close", "histo file", GetFullFileName());
#endif
// close file
tools::waxml::end(*fHnFile);
fHnFile->close();
#ifdef G4VERBOSE
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()->Message("close", "histo file", GetFullFileName());
#endif
return true;
}
//_____________________________________________________________________________
G4bool G4XmlFileManager::CreateNtupleFile(
G4TNtupleDescription<tools::waxml::ntuple>* ntupleDescription)
XmlNtupleDescription* ntupleDescription)
{
G4String ntupleName = ntupleDescription->fNtupleBooking.name();
// get ntuple file name per object (if defined)
auto ntupleFileName = GetNtupleFileName(ntupleDescription);
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()
->Message("create", "ntuple file", GetNtupleFileName(ntupleName));
#endif
auto ntupleFile = new std::ofstream(GetNtupleFileName(ntupleName));
if ( ntupleFile->fail() ) {
delete ntupleFile;
G4ExceptionDescription description;
description << " " << "Cannot open file "
<< GetNtupleFileName(ntupleName);
G4Exception("G4XmlFileManager::CreateNtupleFile()",
"Analysis_W001", JustWarning, description);
return false;
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("create", "ntuple file", ntupleFileName);
}
tools::waxml::begin(*ntupleFile);
ntupleDescription->fFile = ntupleFile;
#ifdef G4VERBOSE
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()
->Message("create", "ntuple file", GetNtupleFileName(ntupleName));
#endif
return true;
// update file name if it is already in use
while ( GetTFile(ntupleFileName, false) ) {
// the file is already in use
auto oldName = ntupleDescription->fFileName;
auto newName = GetBaseName(oldName) + "_bis." + GetExtension(oldName);
ntupleDescription->fFileName = newName;
G4ExceptionDescription description;
description
<< "Ntuple filename " << oldName << " is already in use." << G4endl
<< "It will be replaced with : " << newName;
G4Exception("G4XmlFileManager::CreateFileImpl()",
"Analysis_W001", JustWarning, description);
ntupleFileName = GetNtupleFileName(ntupleDescription);
}
ntupleDescription->fFile = CreateTFile(ntupleFileName);
#ifdef G4VERBOSE
if ( fState.GetVerboseL2() ) {
fState.GetVerboseL2()->Message("create", "ntuple file", ntupleFileName);
}
#endif
return (ntupleDescription->fFile != nullptr);
}
//_____________________________________________________________________________
G4bool G4XmlFileManager::CloseNtupleFile(
G4TNtupleDescription<tools::waxml::ntuple>* ntupleDescription)
XmlNtupleDescription* ntupleDescription)
{
// Do nothing if there is no file
if ( ! ntupleDescription->fFile ) return true;
G4String ntupleName = ntupleDescription->fNtupleBooking.name();
auto finalResult = true;
auto ntupleFileName = GetNtupleFileName(ntupleDescription);
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()
->Message("close", "ntuple file", GetNtupleFileName(ntupleName));
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("close", "ntuple file", ntupleFileName);
}
#endif
// close file
tools::waxml::end(*(ntupleDescription->fFile));
ntupleDescription->fFile->close();
auto result = CloseTFile(ntupleFileName);
finalResult = result && finalResult;
// Notify not empty file
result = SetIsEmpty(ntupleFileName, ! ntupleDescription->fHasFill);
finalResult = result && finalResult;
// Reset file info in ntuple description
ntupleDescription->fFile.reset();
#ifdef G4VERBOSE
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()
->Message("close", "ntuple file", GetNtupleFileName(ntupleName));
if ( fState.GetVerboseL2() ) {
fState.GetVerboseL2()->Message("close", "ntuple file", ntupleFileName);
}
#endif
return true;
}
return result;
}
@@ -0,0 +1,136 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
#include "G4XmlNtupleFileManager.hh"
#include "G4XmlFileManager.hh"
#include "G4XmlNtupleManager.hh"
#include "G4AnalysisManagerState.hh"
#include "G4AnalysisUtilities.hh"
#include "G4Threading.hh"
#include "G4AutoLock.hh"
using std::make_shared;
//_____________________________________________________________________________
G4XmlNtupleFileManager::G4XmlNtupleFileManager(const G4AnalysisManagerState& state)
: G4VNtupleFileManager(state, "csv"),
fFileManager(nullptr),
fNtupleManager(nullptr)
{}
//_____________________________________________________________________________
G4XmlNtupleFileManager::~G4XmlNtupleFileManager()
{}
//
// private methods
//
//_____________________________________________________________________________
G4bool G4XmlNtupleFileManager::CloseNtupleFiles()
{
// Close ntuple files
auto finalResult = true;
auto ntupleVector = fNtupleManager->GetNtupleDescriptionVector();
for ( auto ntupleDescription : ntupleVector) {
auto result = fFileManager->CloseNtupleFile(ntupleDescription);
finalResult = finalResult && result;
}
return finalResult;
}
//
// public methods
//
//_____________________________________________________________________________
std::shared_ptr<G4VNtupleManager> G4XmlNtupleFileManager::CreateNtupleManager()
{
fNtupleManager = std::make_shared<G4XmlNtupleManager>(fState);
fNtupleManager->SetFileManager(fFileManager);
return fNtupleManager;
}
//_____________________________________________________________________________
G4bool G4XmlNtupleFileManager::ActionAtOpenFile(const G4String& /*fileName*/)
{
// Create ntuples if they are booked
// (The files will be created with creating ntuples)
fNtupleManager->CreateNtuplesFromBooking(
fBookingManager->GetNtupleBookingVector());
return true;
}
//_____________________________________________________________________________
G4bool G4XmlNtupleFileManager::ActionAtWrite()
{
auto ntupleVector = fNtupleManager->GetNtupleDescriptionVector();
for ( auto ntuple : ntupleVector ) {
if ( ntuple->fNtuple ) ntuple->fNtuple->write_trailer();
}
return true;
}
//_____________________________________________________________________________
G4bool G4XmlNtupleFileManager::ActionAtCloseFile(G4bool reset)
{
auto finalResult = true;
// Close ntuple files
auto result = CloseNtupleFiles();
finalResult = finalResult && result;
// Reset data
if ( ! reset ) {
result = Reset();
if ( ! result ) {
G4ExceptionDescription description;
description << " " << "Resetting data failed";
G4Exception("G4XmlNtupleFileManager::CloseFile()",
"Analysis_W021", JustWarning, description);
}
finalResult = finalResult && result;
}
return finalResult;
}
//_____________________________________________________________________________
G4bool G4XmlNtupleFileManager::Reset()
{
// Reset histograms and ntuple
return fNtupleManager->Reset(true);
}
+13 -21
View File
@@ -42,7 +42,7 @@ using namespace G4Analysis;
//_____________________________________________________________________________
G4XmlNtupleManager::G4XmlNtupleManager(const G4AnalysisManagerState& state)
: G4TNtupleManager<tools::waxml::ntuple>(state),
: G4TNtupleManager<tools::waxml::ntuple, std::ofstream>(state),
fFileManager(nullptr)
{}
@@ -54,26 +54,9 @@ G4XmlNtupleManager::~G4XmlNtupleManager()
// private methods
//
//_____________________________________________________________________________
void G4XmlNtupleManager::CreateTNtuple(
G4TNtupleDescription<tools::waxml::ntuple>* ntupleDescription,
const G4String& /*name*/, const G4String& /*title*/)
{
// Create ntuple if the file is open (what means here that
// a filename was already set)
if ( fFileManager->GetFileName().size() ) {
if ( fFileManager->CreateNtupleFile(ntupleDescription) ) {
ntupleDescription->fNtuple
= new tools::waxml::ntuple(*(ntupleDescription->fFile));
// ntuple object is deleted when closing a file
fNtupleVector.push_back(ntupleDescription->fNtuple);
}
}
}
//_____________________________________________________________________________
void G4XmlNtupleManager::CreateTNtupleFromBooking(
G4TNtupleDescription<tools::waxml::ntuple>* ntupleDescription)
XmlNtupleDescription* ntupleDescription)
{
// create a file for this ntuple
if ( ! fFileManager->CreateNtupleFile(ntupleDescription) ) return;
@@ -87,7 +70,7 @@ void G4XmlNtupleManager::CreateTNtupleFromBooking(
//_____________________________________________________________________________
void G4XmlNtupleManager::FinishTNtuple(
G4TNtupleDescription<tools::waxml::ntuple>* ntupleDescription,
XmlNtupleDescription* ntupleDescription,
G4bool /*fromBooking*/)
{
@@ -99,11 +82,20 @@ void G4XmlNtupleManager::FinishTNtuple(
CreateTNtupleFromBooking(ntupleDescription);
}
// Return if creating ntuple failed
if ( ! ntupleDescription->fNtuple ) {
G4ExceptionDescription description;
description << "Creating ntuple has failed. ";
G4Exception("G4XmlNtupleManager::FinishTNtuple()",
"Analysis_W022", JustWarning, description);
return;
}
// Write header
G4String path = "/";
path.append(fFileManager->GetNtupleDirectoryName());
ntupleDescription->fNtuple
->write_header(path, ntupleDescription->fNtupleBooking.name(),
ntupleDescription->fNtupleBooking.title());
fFileManager->LockNtupleDirectoryName();
fFileManager->LockDirectoryNames();
}
@@ -28,6 +28,7 @@
#include "G4XmlRFileManager.hh"
#include "G4AnalysisManagerState.hh"
#include "G4AnalysisUtilities.hh"
#include "tools/waxml/begend"
@@ -100,7 +100,7 @@ G4bool G4XmlRNtupleManager::SetNtupleIColumn(G4int ntupleId,
if ( fState.GetVerboseL2() ) {
G4ExceptionDescription description;
description << " ntupleId " << ntupleId << " " << columnName;
fState.GetVerboseL2()->Message("set", "ntuple I colum", description, true);
fState.GetVerboseL2()->Message("set", "ntuple I column", description, true);
}
#endif
@@ -138,7 +138,7 @@ G4bool G4XmlRNtupleManager::SetNtupleFColumn(G4int ntupleId,
if ( fState.GetVerboseL2() ) {
G4ExceptionDescription description;
description << " ntupleId " << ntupleId << " " << columnName;
fState.GetVerboseL2()->Message("set", "ntuple F colum", description, true);
fState.GetVerboseL2()->Message("set", "ntuple F column", description, true);
}
#endif
@@ -176,7 +176,7 @@ G4bool G4XmlRNtupleManager::SetNtupleDColumn(G4int ntupleId,
if ( fState.GetVerboseL2() ) {
G4ExceptionDescription description;
description << " ntupleId " << ntupleId << " " << columnName;
fState.GetVerboseL2()->Message("set", "ntuple D colum", description, true);
fState.GetVerboseL2()->Message("set", "ntuple D column", description, true);
}
#endif