Import Geant4 10.4.0 source tree

This commit is contained in:
Gabriele Cosmo
2017-12-08 12:52:30 +01:00
parent 98e455a940
commit fc6af9e721
2166 changed files with 276760 additions and 100873 deletions
@@ -0,0 +1,377 @@
//
// ********************************************************************
// * 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. *
// ********************************************************************
//
// $Id$
// Author: Ivana Hrivnacova, 20/07/2017 (ivana@ipno.in2p3.fr)
#include "G4Hdf5AnalysisManager.hh"
#include "G4Hdf5FileManager.hh"
#include "G4Hdf5NtupleManager.hh"
#include "G4AnalysisManagerState.hh"
#include "G4Threading.hh"
#include "G4AutoLock.hh"
// mutex in a file scope
namespace {
//Mutex to lock master manager when opening a file
G4Mutex openFileMutex = G4MUTEX_INITIALIZER;
//Mutex to lock master manager when merging H1 histograms
G4Mutex mergeH1Mutex = G4MUTEX_INITIALIZER;
//Mutex to lock master manager when merging H1 histograms
G4Mutex mergeH2Mutex = G4MUTEX_INITIALIZER;
//Mutex to lock master manager when merging H1 histograms
G4Mutex mergeH3Mutex = G4MUTEX_INITIALIZER;
//Mutex to lock master manager when merging P1 profiles
G4Mutex mergeP1Mutex = G4MUTEX_INITIALIZER;
//Mutex to lock master manager when merging P2 profiles
G4Mutex mergeP2Mutex = G4MUTEX_INITIALIZER;
//Mutex to lock master manager when closing a file
G4Mutex closeFileMutex = G4MUTEX_INITIALIZER;
}
G4Hdf5AnalysisManager* G4Hdf5AnalysisManager::fgMasterInstance = nullptr;
G4ThreadLocal G4Hdf5AnalysisManager* G4Hdf5AnalysisManager::fgInstance = nullptr;
//_____________________________________________________________________________
G4Hdf5AnalysisManager* G4Hdf5AnalysisManager::Instance()
{
if ( fgInstance == nullptr ) {
G4bool isMaster = ! G4Threading::IsWorkerThread();
fgInstance = new G4Hdf5AnalysisManager(isMaster);
}
return fgInstance;
}
//_____________________________________________________________________________
G4bool G4Hdf5AnalysisManager::IsInstance()
{
return ( fgInstance != 0 );
}
//_____________________________________________________________________________
G4Hdf5AnalysisManager::G4Hdf5AnalysisManager(G4bool isMaster)
: G4ToolsAnalysisManager("Hdf5", isMaster),
fNtupleManager(nullptr),
fFileManager(nullptr)
{
#ifdef G4MULTITHREADED
#ifndef H5_HAVE_THREADSAFE
G4ExceptionDescription message;
message
<< "Your HDF5 lib is not built with H5_HAVE_THREADSAFE.";
G4Exception("G4Hdf5AnalysisManager::G4Hdf5AnalysisManager",
"Analysis_F001", FatalException, message);
#endif
#endif
if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
G4ExceptionDescription description;
description
<< " "
<< "G4Hdf5AnalysisManager already exists."
<< "Cannot create another instance.";
G4Exception("G4Hdf5AnalysisManager::G4Hdf5AnalysisManager",
"Analysis_F001", FatalException, description);
}
if ( isMaster ) fgMasterInstance = this;
fgInstance = this;
// File manager
fFileManager = std::make_shared<G4Hdf5FileManager>(fState);
SetFileManager(fFileManager);
fFileManager->SetBasketSize(fgkDefaultBasketSize);
// Ntuple manager
fNtupleManager = new G4Hdf5NtupleManager(fState);
fNtupleManager->SetFileManager(fFileManager);
SetNtupleManager(fNtupleManager);
// The managers will be deleted by the base class
}
//_____________________________________________________________________________
G4Hdf5AnalysisManager::~G4Hdf5AnalysisManager()
{
if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
fgInstance = nullptr;
}
//
// private methods
//
//_____________________________________________________________________________
G4bool G4Hdf5AnalysisManager::WriteH1()
{
auto h1Vector = fH1Manager->GetH1Vector();
auto hnVector = fH1Manager->GetHnVector();
if ( ! h1Vector.size() ) return true;
auto result = true;
if ( ! G4Threading::IsWorkerThread() ) {
auto directoryName = fFileManager->GetHistoDirectoryName();
result = WriteHn(h1Vector, hnVector, directoryName, "h1");
}
else {
// The worker manager just adds its histograms to the master
// This operation needs a lock
G4AutoLock lH1(&mergeH1Mutex);
fgMasterInstance->fH1Manager->AddH1Vector(h1Vector);
lH1.unlock();
}
return result;
}
//_____________________________________________________________________________
G4bool G4Hdf5AnalysisManager::WriteH2()
{
auto h2Vector = fH2Manager->GetH2Vector();
auto hnVector = fH2Manager->GetHnVector();
if ( ! h2Vector.size() ) return true;
auto result = true;
if ( ! G4Threading::IsWorkerThread() ) {
auto directoryName = fFileManager->GetHistoDirectoryName();
result = WriteHn(h2Vector, hnVector, directoryName, "h2");
}
else {
// The worker manager just adds its histograms to the master
// This operation needs a lock
G4AutoLock lH2(&mergeH2Mutex);
fgMasterInstance->fH2Manager->AddH2Vector(h2Vector);
lH2.unlock();
}
return result;
}
//_____________________________________________________________________________
G4bool G4Hdf5AnalysisManager::WriteH3()
{
auto h3Vector = fH3Manager->GetH3Vector();
auto hnVector = fH3Manager->GetHnVector();
if ( ! h3Vector.size() ) return true;
auto result = true;
if ( ! G4Threading::IsWorkerThread() ) {
auto directoryName = fFileManager->GetHistoDirectoryName();
result = WriteHn(h3Vector, hnVector, directoryName, "h3");
}
else {
// The worker manager just adds its histograms to the master
// This operation needs a lock
G4AutoLock lH3(&mergeH3Mutex);
fgMasterInstance->fH3Manager->AddH3Vector(h3Vector);
lH3.unlock();
}
return result;
}
//_____________________________________________________________________________
G4bool G4Hdf5AnalysisManager::WriteP1()
{
auto p1Vector = fP1Manager->GetP1Vector();
auto hnVector = fP1Manager->GetHnVector();
if ( ! p1Vector.size() ) return true;
auto result = true;
if ( ! G4Threading::IsWorkerThread() ) {
auto directoryName = fFileManager->GetHistoDirectoryName();
result = WritePn(p1Vector, hnVector, directoryName, "p1");
}
else {
// The worker manager just adds its profiles to the master
// This operation needs a lock
G4AutoLock lP1(&mergeP1Mutex);
fgMasterInstance->fP1Manager->AddP1Vector(p1Vector);
lP1.unlock();
}
return result;
}
//_____________________________________________________________________________
G4bool G4Hdf5AnalysisManager::WriteP2()
{
auto p2Vector = fP2Manager->GetP2Vector();
auto hnVector = fP2Manager->GetHnVector();
if ( ! p2Vector.size() ) return true;
auto result = true;
if ( ! G4Threading::IsWorkerThread() ) {
auto directoryName = fFileManager->GetHistoDirectoryName();
result = WritePn(p2Vector, hnVector, directoryName, "p2");
}
else {
// The worker manager just adds its profiles to the master
// This operation needs a lock
G4AutoLock lP2(&mergeP2Mutex);
fgMasterInstance->fP2Manager->AddP2Vector(p2Vector);
lP2.unlock();
}
return result;
}
//_____________________________________________________________________________
G4bool G4Hdf5AnalysisManager::Reset()
{
// Reset histograms and ntuple
auto finalResult = true;
auto result = G4ToolsAnalysisManager::Reset();
finalResult = finalResult && result;
result = fNtupleManager->Reset(true);
finalResult = finalResult && result;
return finalResult;
}
//
// protected methods
//
//_____________________________________________________________________________
G4bool G4Hdf5AnalysisManager::OpenFileImpl(const G4String& fileName)
{
auto finalResult = true;
auto result = fFileManager->SetFileName(fileName);
finalResult = finalResult && result;
#ifdef G4VERBOSE
G4String name = fFileManager->GetFullFileName();
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("open", "analysis file", name);
#endif
G4AutoLock lock(&openFileMutex);
result = fFileManager->OpenFile(fileName);
finalResult = finalResult && result;
// fNtupleManager->SetNtupleDirectory(fFileManager->GetNtupleDirectory());
fNtupleManager->CreateNtuplesFromBooking();
lock.unlock();
#ifdef G4VERBOSE
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()->Message("open", "analysis file", name, finalResult);
#endif
return finalResult;
}
//_____________________________________________________________________________
G4bool G4Hdf5AnalysisManager::WriteImpl()
{
auto finalResult = true;
#ifdef G4VERBOSE
auto name = fFileManager->GetFullFileName();
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("write", "files", name);
#endif
// Histo directory
// auto result = fFileManager->WriteHistoDirectory();
// if ( ! result ) return false;
auto result = WriteH1();
finalResult = finalResult && result;
// H2
result = WriteH2();
finalResult = finalResult && result;
// H3
result = WriteH3();
finalResult = finalResult && result;
// P1
result = WriteP1();
finalResult = finalResult && result;
// P2
result = WriteP2();
finalResult = finalResult && result;
// // Ntuple directory
// result = fFileManager->WriteNtupleDirectory();
// if ( ! result ) return false;
// Write ASCII if activated
if ( IsAscii() ) {
result = WriteAscii(fFileManager->GetFileName());
finalResult = finalResult && result;
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()
->Message("write", "file", fFileManager->GetFullFileName(), finalResult);
#endif
return finalResult;
}
//_____________________________________________________________________________
G4bool G4Hdf5AnalysisManager::CloseFileImpl()
{
auto finalResult = true;
G4AutoLock lock(&closeFileMutex);
auto result = fFileManager->CloseFile();
finalResult = finalResult && result;
// reset data
result = Reset();
if ( ! result ) {
G4ExceptionDescription description;
description << " " << "Resetting data failed";
G4Exception("G4Hdf5AnalysisManager::CloseFile()",
"Analysis_W021", JustWarning, description);
}
lock.unlock();
finalResult = finalResult && result;
// No files clean-up as ntuples are not supported in MT mode
return finalResult;
}
@@ -0,0 +1,273 @@
//
// ********************************************************************
// * 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. *
// ********************************************************************
//
// $Id$
// Author: Ivana Hrivnacova, 20/07/2017 (ivana@ipno.in2p3.fr)
#include "G4Hdf5AnalysisReader.hh"
#include "G4Hdf5RNtupleManager.hh"
#include "G4AnalysisVerbose.hh"
#include "G4AnalysisUtilities.hh"
#include "G4Threading.hh"
#include <iostream>
#include <cstdio>
using namespace G4Analysis;
G4Hdf5AnalysisReader* G4Hdf5AnalysisReader::fgMasterInstance = nullptr;
G4ThreadLocal G4Hdf5AnalysisReader* G4Hdf5AnalysisReader::fgInstance = nullptr;
//_____________________________________________________________________________
G4Hdf5AnalysisReader* G4Hdf5AnalysisReader::Instance()
{
if ( fgInstance == nullptr ) {
G4bool isMaster = ! G4Threading::IsWorkerThread();
fgInstance = new G4Hdf5AnalysisReader(isMaster);
}
return fgInstance;
}
//_____________________________________________________________________________
G4Hdf5AnalysisReader::G4Hdf5AnalysisReader(G4bool isMaster)
: G4ToolsAnalysisReader("Hdf5", isMaster),
fNtupleManager(nullptr),
fFileManager(nullptr)
{
if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
G4ExceptionDescription description;
description
<< " "
<< "G4Hdf5AnalysisReader already exists."
<< "Cannot create another instance.";
G4Exception("G4Hdf5AnalysisReader::G4Hdf5AnalysisReader()",
"Analysis_F001", FatalException, description);
}
if ( isMaster ) fgMasterInstance = this;
fgInstance = this;
// Create managers
fNtupleManager = new G4Hdf5RNtupleManager(fState);
fFileManager = new G4Hdf5RFileManager(fState);
// Set managers to base class
SetNtupleManager(fNtupleManager);
SetFileManager(fFileManager);
}
//_____________________________________________________________________________
G4Hdf5AnalysisReader::~G4Hdf5AnalysisReader()
{
if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
fgInstance = nullptr;
}
//
// private methods
//
//_____________________________________________________________________________
G4bool G4Hdf5AnalysisReader::Reset()
{
// Reset histograms and ntuple
auto finalResult = true;
auto result = G4ToolsAnalysisReader::Reset();
finalResult = finalResult && result;
result = fNtupleManager->Reset();
finalResult = finalResult && result;
return finalResult;
}
//
// protected methods
//
//_____________________________________________________________________________
G4int G4Hdf5AnalysisReader::ReadH1Impl(const G4String& h1Name,
const G4String& fileName,
const G4String& dirName,
G4bool /*isUserFileName*/)
{
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("read", "h1", h1Name);
#endif
auto h1 = ReadHnImpl<tools::histo::h1d>(h1Name, fileName, dirName);
if ( ! h1 ) return kInvalidId;
auto id = fH1Manager->AddH1(h1Name, h1);
#ifdef G4VERBOSE
if ( fState.GetVerboseL2() )
fState.GetVerboseL2()->Message("read", "h1", h1Name, id > kInvalidId);
#endif
return id;
}
//_____________________________________________________________________________
G4int G4Hdf5AnalysisReader::ReadH2Impl(const G4String& h2Name,
const G4String& fileName,
const G4String& dirName,
G4bool /*isUserFileName*/)
{
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("read", "h2", h2Name);
#endif
auto h2 = ReadHnImpl<tools::histo::h2d>(h2Name, fileName, dirName);
if ( ! h2 ) return kInvalidId;
auto id = fH2Manager->AddH2(h2Name, h2);
#ifdef G4VERBOSE
if ( fState.GetVerboseL2() )
fState.GetVerboseL2()->Message("read", "h2", h2Name, id > kInvalidId);
#endif
return id;
}
//_____________________________________________________________________________
G4int G4Hdf5AnalysisReader::ReadH3Impl(const G4String& h3Name,
const G4String& fileName,
const G4String& dirName,
G4bool /*isUserFileName*/)
{
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("read", "h3", h3Name);
#endif
auto h3 = ReadHnImpl<tools::histo::h3d>(h3Name, fileName, dirName);
if ( ! h3 ) return kInvalidId;
auto id = fH3Manager->AddH3(h3Name, h3);
#ifdef G4VERBOSE
if ( fState.GetVerboseL2() )
fState.GetVerboseL2()->Message("read", "h3", h3Name, id > kInvalidId);
#endif
return id;
}
//_____________________________________________________________________________
G4int G4Hdf5AnalysisReader::ReadP1Impl(const G4String& p1Name,
const G4String& fileName,
const G4String& dirName,
G4bool /*isUserFileName*/)
{
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("read", "p1", p1Name);
#endif
auto p1 = ReadPnImpl<tools::histo::p1d>(p1Name, fileName, dirName);
if ( ! p1 ) return kInvalidId;
auto id = fP1Manager->AddP1(p1Name, p1);
#ifdef G4VERBOSE
if ( fState.GetVerboseL2() )
fState.GetVerboseL2()->Message("read", "p1", p1Name, id > kInvalidId);
#endif
return id;
}
//_____________________________________________________________________________
G4int G4Hdf5AnalysisReader::ReadP2Impl(const G4String& p2Name,
const G4String& fileName,
const G4String& dirName,
G4bool /*isUserFileName*/)
{
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("read", "p2", p2Name);
#endif
auto p2 = ReadPnImpl<tools::histo::p2d>(p2Name, fileName, dirName);
if ( ! p2 ) return kInvalidId;
auto id = fP2Manager->AddP2(p2Name, p2);
#ifdef G4VERBOSE
if ( fState.GetVerboseL2() )
fState.GetVerboseL2()->Message("read", "p2", p2Name, id > kInvalidId);
#endif
return id;
}
//_____________________________________________________________________________
G4int G4Hdf5AnalysisReader::ReadNtupleImpl(const G4String& ntupleName,
const G4String& fileName,
const G4String& dirName,
G4bool isUserFileName)
{
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("read", "ntuple", ntupleName);
#endif
// Ntuples are saved in files per thread
// but apply thethe thread suffix only if fileName is not provided explicitly
G4String fullFileName = fileName;
if ( ! isUserFileName ) {
fullFileName = fFileManager->GetFullFileName();
}
// Get directory
auto directory = fFileManager->GetNtupleRDirectory(fullFileName, dirName, false);
if ( directory < 0 ) return kInvalidId;
// Create ntuple
auto rntuple = new tools::hdf5::ntuple(G4cout, directory, ntupleName);
auto rntupleDescription = new G4TRNtupleDescription<tools::hdf5::ntuple>(rntuple);
auto id = fNtupleManager->SetNtuple(rntupleDescription);
#ifdef G4VERBOSE
if ( fState.GetVerboseL2() )
fState.GetVerboseL2()->Message("read", "ntuple", ntupleName, id > kInvalidId);
#endif
return id;
}
@@ -0,0 +1,267 @@
//
// ********************************************************************
// * 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. *
// ********************************************************************
//
// $Id$
// Author: Ivana Hrivnacova, 20/07/2017 (ivana@ipno.in2p3.fr)
#include "G4Hdf5FileManager.hh"
#include "G4AnalysisManagerState.hh"
#include "G4AnalysisUtilities.hh"
#include "tools/hdf5/h2file"
using namespace G4Analysis;
//_____________________________________________________________________________
const G4String G4Hdf5FileManager::fgkDefaultDirectoryName = "default";
//_____________________________________________________________________________
G4Hdf5FileManager::G4Hdf5FileManager(const G4AnalysisManagerState& state)
: G4VFileManager(state),
fFile(kInvalidId),
fHistoDirectory(kInvalidId),
fNtupleDirectory(kInvalidId),
fBasketSize(0) // TO DO: check default value !! (433 in test)
{}
//_____________________________________________________________________________
G4Hdf5FileManager::~G4Hdf5FileManager()
{}
//
// private methods
//
//_____________________________________________________________________________
G4bool G4Hdf5FileManager::CreateDirectory(const G4String& directoryType,
const G4String& directoryName, hid_t& directory)
{
// Method for both histograms and ntuples directories.
// For histos: CreateDirectory("histograms", fHistoDirectoryName, fHistoDirectory)
// For ntples: CreateDirectory("ntuples", fNtupleDirectoryName, fNtupleDirectory)
auto newDirectoryName = directoryName;
if ( newDirectoryName == "" ) {
// if ( fDefaultDirectory > 0 ) {
// // Return the default directory if the name is not set and the default directory
// // already exists
// directory = fDefaultDirectory;
// return true;
// } else {
// Create the default directory if the name is not set and the default directory
// does not yet exist
newDirectoryName = fgkDefaultDirectoryName;
newDirectoryName += "_";
newDirectoryName += directoryType;
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
G4String message = "directory for ";
message += directoryType;
fState.GetVerboseL4()->Message("create", message, newDirectoryName);
}
#endif
directory = tools_H5Gcreate(fFile, newDirectoryName, 0);
// 0 seems to be an optional parameter. The web doc does not say what should
// be the default value but 0 is what is found in examples, and in the code, if we pass 0, clearly some
// default value is taken.
if ( directory < 0 ) {
G4ExceptionDescription description;
description << " "
<< "cannot create directory " << directoryName;
G4Exception("G4Hdf5FileManager::CreateDirectory()",
"Analysis_W001", JustWarning, description);
return false;
}
#ifdef G4VERBOSE
else {
if ( fState.GetVerboseL2() ) {
G4String message = "directory for ";
message += directoryType;
fState.GetVerboseL2()->Message("create", message, newDirectoryName);
}
}
#endif
return true;
}
//_____________________________________________________________________________
#ifdef G4VERBOSE
G4bool G4Hdf5FileManager::WriteDirectory(const G4String& directoryType,
const G4String& directoryName, hid_t& directory)
#else
G4bool G4Hdf5FileManager::WriteDirectory(const G4String& /*directoryType*/,
const G4String& directoryName, hid_t& directory)
#endif
{
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
G4String message = "directory for ";
message += directoryType;
fState.GetVerboseL4()
->Message("write", message, directoryName);
}
#endif
auto result
= tools::hdf5::write_atb(directory, "type", "directory");
if ( ! result ) {
G4ExceptionDescription description;
description << " "
<< "cannot write directory " << directoryName;
G4Exception("G4Hdf5FileManager::WriteDirectory()",
"Analysis_W001", JustWarning, description);
// close
// ::H5Gclose(histos);
// ::H5Fclose(file);
return false;
}
#ifdef G4VERBOSE
else {
if ( fState.GetVerboseL2() ) {
G4String message = "directory for ";
message += directoryType;
fState.GetVerboseL2()->Message("write", message, fHistoDirectoryName);
}
}
#endif
return result;
}
//
// public methods
//
//_____________________________________________________________________________
G4bool G4Hdf5FileManager::OpenFile(const G4String& fileName)
{
// Keep file name
fFileName = fileName;
auto name = GetFullFileName();
// delete previous file if exists
//if ( fFile ) delete fFile;
// create new file
fFile = ::H5Fcreate(name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
if ( fFile < 0 ) {
G4ExceptionDescription description;
description << " " << "Cannot open file " << fileName;
G4Exception("G4Hdf5AnalysisManager::OpenFile()",
"Analysis_W001", JustWarning, description);
return false;
}
// Create directories
if ( ! CreateDirectory("histograms", fHistoDirectoryName, fHistoDirectory) ) return false;
if ( ! CreateDirectory("ntuples", fNtupleDirectoryName, fNtupleDirectory) ) return false;
// // Open ntuple files
// OpenNtupleFiles();
// Write directories
if ( ! WriteHistoDirectory() ) return false;
if ( ! WriteNtupleDirectory() ) return false;
fLockFileName = true;
fLockHistoDirectoryName = true;
fLockNtupleDirectoryName = true;
fIsOpenFile = true;
return true;
}
//_____________________________________________________________________________
G4bool G4Hdf5FileManager::WriteFile()
{
// Nothing to be done here
return true;
}
//_____________________________________________________________________________
G4bool G4Hdf5FileManager::CloseFile()
{
// Do nothing if there is no file
if ( fFile < 0 ) return true;
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("close", "file", GetFullFileName());
#endif
if ( fHistoDirectory >= 0 ) {
::H5Gclose(fHistoDirectory);
}
if ( fNtupleDirectory >= 0 ) {
::H5Gclose(fNtupleDirectory);
}
::H5Fclose(fFile);
fLockFileName = false;
fIsOpenFile = false;
#ifdef G4VERBOSE
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()->Message("close", "file", GetFullFileName(), true);
#endif
// CHECK
// the result not returned from hdf5
return true;
}
//_____________________________________________________________________________
G4bool G4Hdf5FileManager::WriteHistoDirectory()
{
return WriteDirectory("histograms", fHistoDirectoryName, fHistoDirectory);
}
//_____________________________________________________________________________
G4bool G4Hdf5FileManager::WriteNtupleDirectory()
{
return WriteDirectory("ntuples", fNtupleDirectoryName, fNtupleDirectory);
}
//_____________________________________________________________________________
void G4Hdf5FileManager::CloseAfterHnWrite()
{
if ( fHistoDirectory >= 0 ) {
::H5Gclose(fHistoDirectory);
}
::H5Fclose(fFile);
}
@@ -0,0 +1,104 @@
//
// ********************************************************************
// * 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. *
// ********************************************************************
//
// $Id$
// Author: Ivana Hrivnacova, 20/07/2017 (ivana@ipno.in2p3.fr)
#include "G4Hdf5NtupleManager.hh"
#include "G4Hdf5FileManager.hh"
#include "G4AnalysisManagerState.hh"
#include "G4AnalysisUtilities.hh"
#include "G4UnitsTable.hh"
#include "tools/ntuple_booking"
#include <iostream>
//#include <cstdio>
using namespace G4Analysis;
//_____________________________________________________________________________
G4Hdf5NtupleManager::G4Hdf5NtupleManager(const G4AnalysisManagerState& state)
: G4TNtupleManager<tools::hdf5::ntuple>(state),
fFileManager(nullptr)
{}
//_____________________________________________________________________________
G4Hdf5NtupleManager::~G4Hdf5NtupleManager()
{}
//
// private methods
//
//_____________________________________________________________________________
void G4Hdf5NtupleManager::CreateTNtuple(
G4TNtupleDescription<tools::hdf5::ntuple>* /*ntupleDescription*/,
const G4String& /*name*/, const G4String& /*title*/)
{
// Ntuple will be created at finish ntuple from ntuple_booking
}
//_____________________________________________________________________________
void G4Hdf5NtupleManager::CreateTNtupleFromBooking(
G4TNtupleDescription<tools::hdf5::ntuple>* ntupleDescription)
{
// create a file for this ntuple
// if ( ! fFileManager->CreateNtupleFile(ntupleDescription) ) return;
// Check ntuple directory
if ( fFileManager->GetNtupleDirectory() < 0 ) {
G4String inFunction = "G4Hdf5NtupleManager::::CreateTNtupleFromBooking";
G4ExceptionDescription description;
description << " "
<< "Cannot create ntuple. Ntuple directory does not exist." << G4endl;
G4Exception(inFunction, "Analysis_W002", JustWarning, description);
return;
}
auto basketSize = fFileManager->GetBasketSize();
// auto compressionLevel = fState.GetCompressionLevel();
auto compressionLevel = 0;
// create ntuple
ntupleDescription->fNtuple
= new tools::hdf5::ntuple(
G4cout, fFileManager->GetNtupleDirectory(), ntupleDescription->fNtupleBooking,
compressionLevel, basketSize);
fNtupleVector.push_back(ntupleDescription->fNtuple);
}
//_____________________________________________________________________________
void G4Hdf5NtupleManager::FinishTNtuple(
G4TNtupleDescription<tools::hdf5::ntuple>* /*ntupleDescription*/)
{
// if ( ! ntupleDescription->fNtuple ) return;
// Only ntuple description is created on CreateNtuple call
// CreateTNtupleFromBooking(ntupleDescription);
fFileManager->LockNtupleDirectoryName();
}
@@ -0,0 +1,201 @@
//
// ********************************************************************
// * 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. *
// ********************************************************************
//
// $Id$
// Author: Ivana Hrivnacova, 20/07/2017 (ivana@ipno.in2p3.fr)
#include "G4Hdf5RFileManager.hh"
#include "G4AnalysisManagerState.hh"
#include "G4AnalysisUtilities.hh"
#include "tools/hdf5/h2file"
#include <tools/zlib>
#include <iostream>
#include <cstdio>
using namespace G4Analysis;
//_____________________________________________________________________________
const G4String G4Hdf5RFileManager::fgkDefaultDirectoryName = "default";
//_____________________________________________________________________________
G4Hdf5RFileManager::G4Hdf5RFileManager(const G4AnalysisManagerState& state)
: G4BaseFileManager(state),
fRFiles()
{
}
//_____________________________________________________________________________
G4Hdf5RFileManager::~G4Hdf5RFileManager()
{
}
//
// private methods
//
//_____________________________________________________________________________
hid_t G4Hdf5RFileManager::OpenRFile(const G4String& fileName,
G4bool isPerThread)
{
// Get full file name
G4String name = GetFullFileName(fileName, isPerThread);
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("open", "read analysis file", name);
#endif
// create new file
hid_t newFile = H5Fopen(name, H5F_ACC_RDONLY, H5P_DEFAULT);
if ( newFile < 0 ) {
G4ExceptionDescription description;
description << " " << "Cannot open file " << name;
G4Exception("G4Hdf5RFileManager::OpenFile()",
"Analysis_WR001", JustWarning, description);
return false;
}
// newFile->add_unziper('Z',tools::decompress_buffer);
// add file in a map
std::map<G4String, hid_t>::iterator it
= fRFiles.find(name);
if ( it != fRFiles.end() ) {
it->second = newFile;
}
else {
fRFiles[name] = newFile;
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()
->Message("open", "read analysis file", name);
#endif
return true;
}
//_____________________________________________________________________________
hid_t G4Hdf5RFileManager::OpenDirectory(hid_t file, const G4String& directoryName)
{
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("open", "read directory", directoryName);
}
#endif
auto directory = tools_H5Gopen(file, directoryName);
if ( directory < 0 ) {
G4ExceptionDescription description;
description << " "
<< "cannot open directory " << directoryName;
G4Exception("G4Hdf5RFileManager::OpenDirectory()",
"Analysis_W001", JustWarning, description);
return kInvalidId;
}
else {
#ifdef G4VERBOSE
if ( fState.GetVerboseL2() ) {
fState.GetVerboseL2()->Message("open", "read directory", directoryName);
}
#endif
return directory;
}
}
//_____________________________________________________________________________
hid_t G4Hdf5RFileManager::GetRDirectory(const G4String& directoryType,
const G4String& fileName,
const G4String& dirName,
G4bool isPerThread)
{
// Get or open a file
auto rfile = GetRFile(fileName, isPerThread);
if ( rfile < 0 ) {
// Try to open it if not found in the map
if ( ! OpenRFile(fileName, isPerThread) ) return kInvalidId;
rfile = GetRFile(fileName, isPerThread);
}
// Use default directory name if not specified
auto newDirName = dirName;
if ( newDirName == "" ) {
// if ( fDefaultDirectory > 0 ) {
// // Return the default directory if the name is not set and the default directory
// // already exists
// directory = fDefaultDirectory;
// return true;
// } else {
// Create the default directory if the name is not set and the default directory
// does not yet exist
newDirName = fgkDefaultDirectoryName;
newDirName += "_";
newDirName += directoryType;
}
// Open directory
return OpenDirectory(rfile, newDirName);
}
//
// public methods
//
//_____________________________________________________________________________
hid_t G4Hdf5RFileManager::GetRFile(const G4String& fileName,
G4bool isPerThread) const
{
// Get full file name
G4String name = GetFullFileName(fileName, isPerThread);
std::map<G4String, hid_t>::const_iterator it
= fRFiles.find(name);
if ( it != fRFiles.end() )
return it->second;
else {
return kInvalidId;
}
}
//_____________________________________________________________________________
hid_t G4Hdf5RFileManager::GetHistoRDirectory(const G4String& fileName, const G4String& dirName,
G4bool isPerThread)
{
return GetRDirectory("histograms", fileName, dirName, isPerThread);
}
//_____________________________________________________________________________
hid_t G4Hdf5RFileManager::GetNtupleRDirectory(const G4String& fileName, const G4String& dirName,
G4bool isPerThread)
{
return GetRDirectory("ntuples", fileName, dirName, isPerThread);
}
@@ -0,0 +1,77 @@
//
// ********************************************************************
// * 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. *
// ********************************************************************
//
// $Id$
// Author: Ivana Hrivnacova, 20/07/2017 (ivana@ipno.in2p3.fr)
#include "G4Hdf5RNtupleManager.hh"
//_____________________________________________________________________________
G4Hdf5RNtupleManager::G4Hdf5RNtupleManager(const G4AnalysisManagerState& state)
: G4TRNtupleManager<tools::hdf5::ntuple>(state)
{}
//_____________________________________________________________________________
G4Hdf5RNtupleManager::~G4Hdf5RNtupleManager()
{}
//
// private methods
//
//_____________________________________________________________________________
G4bool G4Hdf5RNtupleManager::GetTNtupleRow(
G4TRNtupleDescription<tools::hdf5::ntuple>* ntupleDescription)
{
auto ntuple = ntupleDescription->fNtuple;
auto isInitialized = ntupleDescription->fIsInitialized;
if ( ! isInitialized ) {
auto ntupleBinding = ntupleDescription->fNtupleBinding;
if ( ! ntuple->initialize(G4cout, *ntupleBinding) ) {
G4ExceptionDescription description;
description
<< " "
<< "Ntuple initialization failed !!";
G4Exception("G4Hdf5RNtuple::GetNtupleRow()",
"Analysis_WR021", JustWarning, description);
return false;
}
ntupleDescription->fIsInitialized = true;
}
if ( ! ntuple->get_row() ) {
G4ExceptionDescription description;
description
<< " "
<< "Ntuple get_row() failed !!";
G4Exception("G4Hdf5RNtuple::GetNtupleRow()",
"Analysis_WR021", JustWarning, description);
return false;
}
return true;
}