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
@@ -0,0 +1,114 @@
//
// ********************************************************************
// * 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 main manager for Root analysis.
// It delegates most of functions to the object specific managers.
// Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
#ifndef G4GenericAnalysisManager_h
#define G4GenericAnalysisManager_h 1
#include "G4ToolsAnalysisManager.hh"
#include "G4THnManager.hh"
#include "G4Threading.hh"
#include "globals.hh"
#include <memory>
class G4GenericFileManager;
class G4HnInformation;
class G4VNtupleFileManager;
class G4GenericAnalysisManager : public G4ToolsAnalysisManager
{
friend class G4RootMpiAnalysisManager;
public:
explicit G4GenericAnalysisManager(G4bool isMaster = true);
virtual ~G4GenericAnalysisManager();
// static methods
static G4GenericAnalysisManager* Instance();
static G4bool IsInstance();
// MT/MPI
virtual void SetNtupleMerging(G4bool mergeNtuples,
G4int nofReducedNtupleFiles = 0) override;
virtual void SetNtupleRowWise(G4bool rowWise, G4bool rowMode = true) override;
virtual void SetBasketSize(unsigned int basketSize) override;
virtual void SetBasketEntries(unsigned int basketEntries) override;
// new functions
G4bool Merge();
// write in an extra file
G4bool WriteH1(G4int id, const G4String& fileName);
G4bool WriteH2(G4int id, const G4String& fileName);
G4bool WriteH3(G4int id, const G4String& fileName);
G4bool WriteP1(G4int id, const G4String& fileName);
G4bool WriteP2(G4int id, const G4String& fileName);
// set default output type (backward compatibility)
// this type will be used for file names without extension
void SetDefaultFileType(const G4String& value);
G4String GetDefaultFileType() const;
protected:
// virtual methods from base class
virtual G4bool OpenFileImpl(const G4String& fileName) override;
virtual G4bool WriteImpl() final;
virtual G4bool CloseFileImpl(G4bool reset = true) override;
virtual G4bool IsOpenFileImpl() const final;
virtual G4bool Reset() final;
private:
// methods
void CreateNtupleFileManager(const G4String& fileName);
// static data members
static G4GenericAnalysisManager* fgMasterInstance;
static G4ThreadLocal G4GenericAnalysisManager* fgInstance;
// data members
std::shared_ptr<G4GenericFileManager> fFileManager;
// add G4GenericNtupleManager
// this class will be analogic to file managers but with ntuples
std::shared_ptr<G4VNtupleFileManager> fNtupleFileManager;
// data members
G4bool fIsNtupleMergingSet = false;
G4int fNofNtupleFiles = 0;
G4bool fMergeNtuples = false;
G4bool fNtupleRowWise = false;
G4bool fNtupleRowMode = true;
unsigned int fBasketSize = fgkDefaultBasketSize;
unsigned int fBasketEntries = fgkDefaultBasketEntries;
};
#include "G4GenericAnalysisManager.icc"
#endif
@@ -0,0 +1,78 @@
//
// ********************************************************************
// * 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. *
// ********************************************************************
//
#include "G4GenericFileManager.hh"
#include "G4AutoLock.hh"
//_____________________________________________________________________________
inline void G4GenericAnalysisManager::SetNtupleMerging(
G4bool mergeNtuples, G4int nofReducedNtupleFiles)
{
fIsNtupleMergingSet = true;
fMergeNtuples = mergeNtuples;
fNofNtupleFiles = nofReducedNtupleFiles;
}
//_____________________________________________________________________________
inline void G4GenericAnalysisManager::SetNtupleRowWise(
G4bool rowWise, G4bool rowMode)
{
fNtupleRowWise = rowWise;
fNtupleRowMode = rowMode;
}
//_____________________________________________________________________________
inline void G4GenericAnalysisManager::SetBasketSize(
unsigned int basketSize)
{
fBasketSize = basketSize;
}
//_____________________________________________________________________________
inline void G4GenericAnalysisManager::SetBasketEntries(
unsigned int basketEntries)
{
fBasketEntries = basketEntries;
}
//_____________________________________________________________________________
inline void G4GenericAnalysisManager::SetDefaultFileType(const G4String& value)
{
fFileManager->SetDefaultFileType(value);
}
//_____________________________________________________________________________
inline G4String G4GenericAnalysisManager::GetDefaultFileType() const
{
return fFileManager->GetDefaultFileType();
}
//_____________________________________________________________________________
inline
G4bool G4GenericAnalysisManager::IsOpenFileImpl() const
{
return fFileManager->IsOpenFile();
}
@@ -0,0 +1,115 @@
//
// ********************************************************************
// * 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 for Root output file operations.
// Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
#ifndef G4GenericFileManager_h
#define G4GenericFileManager_h 1
#include "G4VFileManager.hh"
#include "G4AnalysisUtilities.hh"
#include "globals.hh"
#include <vector>
#include <memory>
class G4HnInformation;
class G4VNtupleFileManager;
class G4CsvFileManager;
#ifdef TOOLS_USE_HDF5
class G4Hdf5FileManager;
#endif
class G4RootFileManager;
class G4XmlFileManager;
class G4GenericFileManager : public G4VFileManager
{
public:
explicit G4GenericFileManager(const G4AnalysisManagerState& state);
virtual ~G4GenericFileManager();
// Method to manipulate a default file
virtual G4bool OpenFile(const G4String& fileName) final;
// Methods applied to all registered files
virtual G4bool OpenFiles() final;
virtual G4bool WriteFiles() final;
virtual G4bool CloseFiles() final;
virtual G4bool DeleteEmptyFiles() final;
// Methods applied to file per name
virtual G4bool CreateFile(const G4String& fileName) final;
virtual G4bool WriteFile(const G4String& fileName) final;
virtual G4bool CloseFile(const G4String& fileName) final;
virtual G4bool SetIsEmpty(const G4String& fileName, G4bool isEmpty) final;
virtual G4String GetFileType() const final { return ""; }
// set default output type (backward compatibility)
// this type will be used for file names without extension
void SetDefaultFileType(const G4String& value);
G4String GetDefaultFileType() const;
template <typename T>
G4bool WriteT(const std::vector<T*>& htVector,
const std::vector<G4HnInformation*>& hnVector);
template <typename T>
G4bool WriteTExtra(const G4String& fileName, T* ht, const G4String& htName);
// methods
std::shared_ptr<G4VNtupleFileManager> CreateNtupleFileManager(G4AnalysisOutput output);
private:
// methods
void CreateFileManager(G4AnalysisOutput output);
std::shared_ptr<G4VFileManager> GetFileManager(G4AnalysisOutput output) const;
std::shared_ptr<G4VFileManager> GetFileManager(const G4String& fileName);
// constants
static const G4String fgkDefaultFileType;
// data members
G4String fDefaultFileType;
std::shared_ptr<G4VFileManager> fDefaultFileManager;
std::vector<std::shared_ptr<G4VFileManager>> fFileManagers;
std::shared_ptr<G4CsvFileManager> fCsvFileManager;
#ifdef TOOLS_USE_HDF5
std::shared_ptr<G4Hdf5FileManager> fHdf5FileManager;
#endif
std::shared_ptr<G4RootFileManager> fRootFileManager;
std::shared_ptr<G4XmlFileManager> fXmlFileManager;
G4bool fHdf5Warn;
};
#include "G4GenericFileManager.icc"
#endif
@@ -0,0 +1,154 @@
//
// ********************************************************************
// * 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. *
// ********************************************************************
//
#include "G4HnInformation.hh"
//_____________________________________________________________________________
inline G4String G4GenericFileManager::GetDefaultFileType() const
{
return fDefaultFileType;
}
//_____________________________________________________________________________
template <typename T>
inline
G4bool G4GenericFileManager::WriteT(const std::vector<T*>& htVector,
const std::vector<G4HnInformation*>& hnVector)
{
auto finalResult = true;
for ( G4int i=0; i<G4int(htVector.size()); ++i ) {
auto info = hnVector[i];
auto activation = info->GetActivation();
// skip writing if activation is enabled and H1 is inactivated
if ( fState.GetIsActivation() && ( ! activation ) ) continue;
auto name = info->GetName();
auto ht = htVector[i];
auto fileName = info->GetFileName();
auto fileManager = fDefaultFileManager;
auto fileKind = "default";
if ( fileName == "" ) {
fileName = fileManager->GetFileName();
} else {
fileManager = GetFileManager(fileName);
fileKind = "extra";
// skip writing if cannot get file manager (wrong file extension)
if ( ! fileManager ) {
if ( G4Analysis::GetExtension(fileName) != "hdf5" || fHdf5Warn ) {
G4ExceptionDescription description;
description
<< "Cannot get file manager for " << fileKind << " file " << fileName << "." << G4endl
<< "Writing " << G4Analysis::GetHnType<T>() << " " << name
<< " will be skipped.";
G4Exception("G4GenericFileManager::WriteT",
"Analysis_W022", JustWarning, description);
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL3() ) {
G4ExceptionDescription description;
description << " " << name << " in the " << fileKind << " file " << fileName;
fState.GetVerboseL3()
->Message("write", G4Analysis::GetHnType<T>(), description, false);
}
#endif
continue;
}
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
G4ExceptionDescription description;
description << " " << name << " in the " << fileKind << " file " << fileName;
fState.GetVerboseL3()
->Message("write", G4Analysis::GetHnType<T>(), description);
}
#endif
auto result = fileManager->GetHnFileManager<T>()->Write(ht, name, fileName);
if ( ! result ) {
G4ExceptionDescription description;
description
<< "Writing " << G4Analysis::GetHnType<T>() << " " << name
<< " to file " << fileName << " failed.";
G4Exception("G4GenericManager::WriteT()",
"Analysis_W022", JustWarning, description);
}
finalResult = result && finalResult;
// notify that file has a written object
fileManager->SetIsEmpty(fileName, false);
#ifdef G4VERBOSE
if ( fState.GetVerboseL3() ) {
G4ExceptionDescription description;
description << " " << name << " in the " << fileKind << " file " << fileName;
fState.GetVerboseL3()
->Message("write", G4Analysis::GetHnType<T>(), description, finalResult);
}
#endif
}
return finalResult;
}
//_____________________________________________________________________________
template <typename T>
inline
G4bool G4GenericFileManager::WriteTExtra(
const G4String& fileName, T* ht, const G4String& htName)
{
if ( fState.GetVerboseL4() ) {
G4ExceptionDescription description;
description
<< fileName << " with " << G4Analysis::GetHnType<T>() << " " << htName;
fState.GetVerboseL4()->Message("write", "extra file", description);
}
std::shared_ptr<G4VFileManager> fileManager = GetFileManager(fileName);
if ( ! fileManager ) {
G4ExceptionDescription description;
description
<< "Cannot get file manager for file " << fileName << "." << G4endl
<< "Writing " << G4Analysis::GetHnType<T>() << " " << htName
<< " failed.";
G4Exception("G4GenericFileManager::WriteTExtra",
"Analysis_W022", JustWarning, description);
return false;
}
auto result = fileManager->GetHnFileManager<T>()->WriteExtra(ht, htName, fileName);
#ifdef G4VERBOSE
if ( fState.GetVerboseL1() ) {
fState.GetVerboseL1()->Message("write", "extra file", fileName, result);
}
#endif
return result;
}
+7
View File
@@ -15,12 +15,19 @@ geant4_define_module(NAME G4analysisfac
HEADERS
g4analysis.hh
g4analysis_defs.hh
G4GenericAnalysisManager.hh
G4GenericAnalysisManager.icc
G4GenericFileManager.hh
G4GenericFileManager.icc
SOURCES
g4analysis.cc
G4GenericAnalysisManager.cc
G4GenericFileManager.cc
GRANULAR_DEPENDENCIES
G4globman
G4intercoms
G4analysismng
G4hntools
G4csv
G4root
G4xml
@@ -0,0 +1,495 @@
//
// ********************************************************************
// * 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 "G4GenericAnalysisManager.hh"
#include "G4GenericFileManager.hh"
#include "G4AnalysisVerbose.hh"
#include "G4AnalysisManagerState.hh"
#include "G4AnalysisUtilities.hh"
#include "G4NtupleBookingManager.hh"
#include "G4VNtupleFileManager.hh"
#include <iostream>
#include <cstdio>
using namespace G4Analysis;
// mutex in a file scope
namespace {
//Mutex to lock master manager when merging histograms
G4Mutex mergeHnMutex = G4MUTEX_INITIALIZER;
void WriteHnException(const G4String& hnType, G4int id)
{
G4String inFunction = "G4GenericAnalysisManager::Write" + hnType;
G4ExceptionDescription description;
description << "Failed to get " << hnType << " id " << id << G4endl;
G4Exception(inFunction, "Analysis_W022", JustWarning, description);
}
}
G4GenericAnalysisManager* G4GenericAnalysisManager::fgMasterInstance = nullptr;
G4ThreadLocal G4GenericAnalysisManager* G4GenericAnalysisManager::fgInstance = nullptr;
//_____________________________________________________________________________
G4GenericAnalysisManager* G4GenericAnalysisManager::Instance()
{
if ( fgInstance == nullptr ) {
G4bool isMaster = ! G4Threading::IsWorkerThread();
fgInstance = new G4GenericAnalysisManager(isMaster);
}
return fgInstance;
}
//_____________________________________________________________________________
G4bool G4GenericAnalysisManager::IsInstance()
{
return ( fgInstance != nullptr );
}
//_____________________________________________________________________________
G4GenericAnalysisManager::G4GenericAnalysisManager(G4bool isMaster)
: G4ToolsAnalysisManager("", isMaster),
fFileManager(nullptr),
fNtupleFileManager(nullptr)
{
if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
G4ExceptionDescription description;
description
<< " "
<< "G4GenericAnalysisManager already exists."
<< "Cannot create another instance.";
G4Exception("G4GenericAnalysisManager::G4GenericAnalysisManager()",
"Analysis_F001", FatalException, description);
}
if ( isMaster ) fgMasterInstance = this;
fgInstance = this;
// File manager
fFileManager = std::make_shared<G4GenericFileManager>(fState);
SetFileManager(fFileManager);
}
//_____________________________________________________________________________
G4GenericAnalysisManager::~G4GenericAnalysisManager()
{
if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
fgInstance = nullptr;
}
//
// private methods
//
//_____________________________________________________________________________
void G4GenericAnalysisManager::CreateNtupleFileManager(const G4String& fileName)
{
if ( fNtupleFileManager ) {
G4ExceptionDescription description;
description
<< " "
<< "The ntuple file manager already exists.";
G4Exception("G4GenericAnalysisManager::CreateNtupleFileManager",
"Analysis_W002", JustWarning, description);
return;
}
auto fileType = GetExtension(fileName);
auto output = G4Analysis::GetOutput(fileType);
if ( output == G4AnalysisOutput::kNone ) {
G4ExceptionDescription description;
description
<< "The file type " << fileType << "is not supported.";
G4Exception("G4GenericAnalysisManager::CreateNtupleFileManager",
"Analysis_W051", JustWarning, description);
return;
}
// Set file type to booked ntuples
fNtupleBookingManager->SetFileType(fileType);
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("create", "ntuple file manager", fileType);
}
#endif
fNtupleFileManager = fFileManager->CreateNtupleFileManager(output);
if (fNtupleFileManager) {
fNtupleFileManager->SetBookingManager(fNtupleBookingManager);
}
if ( fNtupleFileManager->IsNtupleMergingSupported() ) {
// set merginng
fNtupleFileManager->SetNtupleMerging(fMergeNtuples, fNofNtupleFiles);
fNtupleFileManager->SetNtupleRowWise(fNtupleRowWise, fNtupleRowMode);
fNtupleFileManager->SetBasketSize(fBasketSize);
fNtupleFileManager->SetBasketEntries(fBasketEntries);
}
else if ( fIsNtupleMergingSet && fMergeNtuples ) {
G4ExceptionDescription description;
description
<< " " << "Ntuple merging is not available with "
<< fileType << " output." << G4endl
<< " " << "Setting is ignored.";
G4Exception("G4GenericAnalysisManager::CreateNtupleFileManager",
"Analysis_W041", JustWarning, description);
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL3() ) {
fState.GetVerboseL3()->Message("create", "ntuple file manager", fileType, true);
}
#endif
}
//
// protected methods
//
//_____________________________________________________________________________
G4bool G4GenericAnalysisManager::OpenFileImpl(const G4String& fileName)
{
// Add file name extension, if missing
auto fullFileName = fileName;
if ( ! GetExtension(fileName).size() ) {
// G4cout << "File type is not defined, using default: " << fFileManager->GetDefaultFileType() << G4endl;
fullFileName = fileName + "." + fFileManager->GetDefaultFileType();
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("open (generic)", "file", fileName);
}
#endif
// Create ntuple file manager if there are booked ntuples
if (! fNtupleFileManager) {
CreateNtupleFileManager(fullFileName);
}
// Create ntuple manager
// and set it to base class which takes then its ownership
if (fNtupleFileManager) {
SetNtupleManager(fNtupleFileManager->CreateNtupleManager());
}
auto finalResult = true;
// Open file for histograms/profiles
auto result = fFileManager->OpenFile(fullFileName);
finalResult = finalResult && result;
// Open ntuple file(s) and create ntuples from bookings
if (fNtupleFileManager) {
result = fNtupleFileManager->ActionAtOpenFile(fullFileName);
finalResult = finalResult && result;
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL3() ) {
fState.GetVerboseL3()->Message("open (generic)", "file", fileName, finalResult);
}
#endif
return finalResult;
}
//_____________________________________________________________________________
G4bool G4GenericAnalysisManager::WriteImpl()
{
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("write (generic)", "files", "");
}
#endif
auto finalResult = true;
auto result = true;
if ( ! fgMasterInstance &&
( ( ! fH1Manager->IsEmpty() ) || ( ! fH2Manager->IsEmpty() ) ||
( ! fH3Manager->IsEmpty() ) || ( ! fP1Manager->IsEmpty() ) ||
( ! fP2Manager->IsEmpty() ) ) ) {
G4ExceptionDescription description;
description
<< " " << "No master G4GenericAnalysisManager instance exists."
<< G4endl
<< " " << "Histogram/profile data will not be merged.";
G4Exception("G4GenericAnalysisManager::Write()",
"Analysis_W031", JustWarning, description);
}
if ( G4Threading::IsWorkerThread() ) {
result = Merge();
finalResult = finalResult && result;
}
else {
// GO ON
// Open all files registered with objects
fFileManager->OpenFiles();
// Write all histograms/profile on master
//
result = fFileManager->WriteT(fH1Manager->GetH1Vector(), fH1Manager->GetHnVector());
finalResult = finalResult && result;
result = fFileManager->WriteT(fH2Manager->GetH2Vector(), fH2Manager->GetHnVector());
finalResult = finalResult && result;
result = fFileManager->WriteT(fH3Manager->GetH3Vector(), fH3Manager->GetHnVector());
finalResult = finalResult && result;
result = fFileManager->WriteT(fP1Manager->GetP1Vector(), fP1Manager->GetHnVector());
finalResult = finalResult && result;
result = fFileManager->WriteT(fP2Manager->GetP2Vector(), fP2Manager->GetHnVector());
finalResult = finalResult && result;
}
// Ntuples
if (fNtupleFileManager) {
result = fNtupleFileManager->ActionAtWrite();
finalResult = finalResult && result;
}
// File
result = fFileManager->WriteFiles();
finalResult = finalResult && result;
// Write ASCII if activated
if ( IsAscii() ) {
result = WriteAscii(fFileManager->GetFileName());
finalResult = finalResult && result;
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL3() ) {
fState.GetVerboseL3()->Message("write (generic)", "files", "", finalResult);
}
#endif
return finalResult;
}
//_____________________________________________________________________________
G4bool G4GenericAnalysisManager::CloseFileImpl(G4bool reset)
{
auto finalResult = true;
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("close (generic)", "files", "");
}
#endif
if (fNtupleFileManager) {
auto result = fNtupleFileManager->ActionAtCloseFile(reset);
finalResult = finalResult && result;
}
// close file
auto result = fFileManager->CloseFiles();
if ( ! result ) {
G4ExceptionDescription description;
description << " " << "Closing files failed";
G4Exception("G4GenericAnalysisManager::CloseFile()",
"Analysis_W021", JustWarning, description);
}
finalResult = finalResult && result;
// delete empty files
result = fFileManager->DeleteEmptyFiles();
if ( ! result ) {
G4ExceptionDescription description;
description << " " << "Deleting empty files failed";
G4Exception("G4GenericAnalysisManager::CloseFile()",
"Analysis_W021", JustWarning, description);
}
finalResult = finalResult && result;
if ( reset ) {
result = Reset();
if ( ! result ) {
G4ExceptionDescription description;
description << " " << "Resetting data failed";
G4Exception("G4GenericAnalysisManager::CloseFile()",
"Analysis_W021", JustWarning, description);
}
}
finalResult = finalResult && result;
#ifdef G4VERBOSE
if ( fState.GetVerboseL3() ) {
fState.GetVerboseL3()->Message("close (generic)", "files", "", finalResult);
}
#endif
return finalResult;
}
//_____________________________________________________________________________
G4bool G4GenericAnalysisManager::Reset()
{
// Reset histograms and ntuple
auto finalResult = true;
auto result = G4ToolsAnalysisManager::Reset();
finalResult = finalResult && result;
result = fNtupleFileManager->Reset();
finalResult = result && finalResult;
return finalResult;
}
//_____________________________________________________________________________
G4bool G4GenericAnalysisManager::Merge()
{
// Nothing to be done on master
if ( ! G4Threading::IsWorkerThread() ) return false;
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("merge (generic) on worker", "histograms", "");
}
#endif
// The worker manager just adds its histograms to the master
fH1Manager->Merge(mergeHnMutex, fgMasterInstance->fH1Manager);
fH2Manager->Merge(mergeHnMutex, fgMasterInstance->fH2Manager);
fH3Manager->Merge(mergeHnMutex, fgMasterInstance->fH3Manager);
fP1Manager->Merge(mergeHnMutex, fgMasterInstance->fP1Manager);
fP2Manager->Merge(mergeHnMutex, fgMasterInstance->fP2Manager);
#ifdef G4VERBOSE
if ( fState.GetVerboseL3() ) {
fState.GetVerboseL3()->Message("merge (generic) on worker", "histograms", "", true);
}
#endif
return true;
}
//_____________________________________________________________________________
G4bool G4GenericAnalysisManager::WriteH1(G4int id, const G4String& fileName)
{
// Experimental extra write
// Do not write histo on worker (redundant and fails in hdf5 )
// If default file is not used, users have to call Merge from their code
if ( G4Threading::IsWorkerThread() ) return false;
auto h1d = GetH1(id, false);
if ( ! h1d ) {
WriteHnException("H1", id);
return false;
}
auto h1Name = GetH1Name(id);
return fFileManager->WriteTExtra<tools::histo::h1d>(fileName, h1d, h1Name);
}
//_____________________________________________________________________________
G4bool G4GenericAnalysisManager::WriteH2(G4int id, const G4String& fileName)
{
// Experimental extra write
// Do not write histo on worker (redundant and fails in hdf5 )
// If default file is not used, users have to call Merge from their code
if ( G4Threading::IsWorkerThread() ) return false;
auto h2d = GetH2(id, false);
if ( ! h2d ) {
WriteHnException("H2", id);
return false;
}
auto h2Name = GetH2Name(id);
return fFileManager->WriteTExtra<tools::histo::h2d>(fileName, h2d, h2Name);
}
//_____________________________________________________________________________
G4bool G4GenericAnalysisManager::WriteH3(G4int id, const G4String& fileName)
{
// Experimental extra write
// Do not write histo on worker (redundant and fails in hdf5 )
// If default file is not used, users have to call Merge from their code
if ( G4Threading::IsWorkerThread() ) return false;
auto h3d = GetH3(id, false);
if ( ! h3d ) {
WriteHnException("H3", id);
return false;
}
auto h3Name = GetH3Name(id);
return fFileManager->WriteTExtra<tools::histo::h3d>(fileName, h3d, h3Name);
}
//_____________________________________________________________________________
G4bool G4GenericAnalysisManager::WriteP1(G4int id, const G4String& fileName)
{
// Experimental extra write
// Do not write histo on worker (redundant and fails in hdf5 )
// If default file is not used, users have to call Merge from their code
if ( G4Threading::IsWorkerThread() ) return false;
auto p1d = GetP1(id, false);
if ( ! p1d ) {
WriteHnException("P1", id);
return false;
}
auto p1Name = GetP1Name(id);
return fFileManager->WriteTExtra<tools::histo::p1d>(fileName, p1d, p1Name);
}
//_____________________________________________________________________________
G4bool G4GenericAnalysisManager::WriteP2(G4int id, const G4String& fileName)
{
// Experimental extra write
// Do not write histo on worker (redundant and fails in hdf5 )
// If default file is not used, users have to call Merge from their code
if ( G4Threading::IsWorkerThread() ) return false;
auto p2d = GetP2(id, false);
if ( ! p2d ) {
WriteHnException("P2", id);
return false;
}
auto p2Name = GetP2Name(id);
return fFileManager->WriteTExtra<tools::histo::p2d>(fileName, p2d, p2Name);
}
@@ -0,0 +1,521 @@
//
// ********************************************************************
// * 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/06/2011 (ivana@ipno.in2p3.fr)
#include "G4GenericFileManager.hh"
#include "G4AnalysisManagerState.hh"
#include "G4AnalysisUtilities.hh"
#include "G4CsvFileManager.hh"
#include "G4CsvNtupleFileManager.hh"
#ifdef TOOLS_USE_HDF5
#include "G4Hdf5FileManager.hh"
#include "G4Hdf5NtupleFileManager.hh"
#endif
#include "G4RootFileManager.hh"
#include "G4RootNtupleFileManager.hh"
#include "G4XmlFileManager.hh"
#include "G4XmlNtupleFileManager.hh"
#include <iostream>
#include <cstdio>
using namespace G4Analysis;
namespace {
void FileManagerException(const G4String& fileName, const G4String& functionName,
const G4String& exceptionClassification, G4bool hdf5Warn = true)
{
if ( GetExtension(fileName) == "hdf5" && ( ! hdf5Warn ) ) return;
G4String where = "G4GenericFileManager::" + functionName;
G4String what = "Analysis_" + exceptionClassification;
G4ExceptionDescription description;
description << "Cannot get file manager for " << fileName;
G4Exception(where, what, JustWarning, description);
}
}
// static data
const G4String G4GenericFileManager::fgkDefaultFileType = "root";
//_____________________________________________________________________________
G4GenericFileManager::G4GenericFileManager(const G4AnalysisManagerState& state)
: G4VFileManager(state),
fDefaultFileType(fgkDefaultFileType),
fDefaultFileManager(nullptr),
fFileManagers
{ nullptr, // Csv
nullptr, // Hdf5
nullptr, // Generic
nullptr // Xml
},
fCsvFileManager(nullptr),
#ifdef TOOLS_USE_HDF5
fHdf5FileManager(nullptr),
#endif
fRootFileManager(nullptr),
fXmlFileManager(nullptr),
fHdf5Warn(true)
{}
//_____________________________________________________________________________
G4GenericFileManager::~G4GenericFileManager()
{}
//
// private methods
//
//_____________________________________________________________________________
void G4GenericFileManager::CreateFileManager(G4AnalysisOutput output)
{
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("create", "file manager", GetOutputName(output));
}
#endif
auto outputId = static_cast<size_t>(output);
if ( fFileManagers[outputId] ) {
G4ExceptionDescription description;
description
<< " "
<< "The file manager of " << G4Analysis::GetOutputName(output) << " type already exists.";
G4Exception("G4GenericFileManager::CreateFileManager",
"Analysis_W002", JustWarning, description);
return;
}
// Create the manager
switch ( output ) {
case G4AnalysisOutput::kCsv:
fCsvFileManager = std::make_shared<G4CsvFileManager>(fState);
fFileManagers[outputId] = fCsvFileManager;
break;
case G4AnalysisOutput::kHdf5:
#ifdef TOOLS_USE_HDF5
fHdf5FileManager = std::make_shared<G4Hdf5FileManager>(fState);
fFileManagers[outputId] = fHdf5FileManager;
#else
if ( fHdf5Warn) {
G4ExceptionDescription description;
description << "Hdf5 type is not available.";
G4Exception("G4GenericFileManager::CreateFileManager",
"Analysis_W051", JustWarning, description);
fHdf5Warn = false;
}
#endif
break;
case G4AnalysisOutput::kRoot:
fRootFileManager = std::make_shared<G4RootFileManager>(fState);
fFileManagers[outputId] = fRootFileManager;
break;
case G4AnalysisOutput::kXml:
fXmlFileManager = std::make_shared<G4XmlFileManager>(fState);
fFileManagers[outputId] = fXmlFileManager ;
break;
case G4AnalysisOutput::kNone:
G4ExceptionDescription description;
description
<< G4Analysis::GetOutputName(output) << " type is not supported.";
G4Exception("G4GenericFileManager::CreateFileManager",
"Analysis_W051", JustWarning, description);
break;
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL3() ) {
fState.GetVerboseL3()->Message("create", "file manager", GetOutputName(output));
}
#endif
}
//_____________________________________________________________________________
std::shared_ptr<G4VFileManager>
G4GenericFileManager::GetFileManager(G4AnalysisOutput output) const
{
return fFileManagers[static_cast<size_t>(output)];
}
//_____________________________________________________________________________
std::shared_ptr<G4VFileManager>
G4GenericFileManager::GetFileManager(const G4String& fileName)
{
// Get file extension
G4String extension = GetExtension(fileName);
if ( ! extension.size() ) {
// use the default
extension = fDefaultFileType;
}
auto output = G4Analysis::GetOutput(extension);
if ( output == G4AnalysisOutput::kNone ) {
G4ExceptionDescription description;
description
<< " "
<< "The file extension " << extension << "is not supported.";
G4Exception("G4GenericFileManager::GetFileManager",
"Analysis_W051", JustWarning, description);
return nullptr;
}
std::shared_ptr<G4VFileManager> fileManager = GetFileManager(output);
if ( ! GetFileManager(output) ) {
CreateFileManager(output);
fileManager = GetFileManager(output);
}
return GetFileManager(output);
}
//
// public methods
//
//_____________________________________________________________________________
G4bool G4GenericFileManager::OpenFile(const G4String& fileName)
{
auto fileManager = GetFileManager(fileName);
if ( ! fileManager ) return false;
if ( fDefaultFileManager && (fDefaultFileManager != fileManager) ) {
// Print warning if default output changed
// (maybe be not needed?)
G4ExceptionDescription description;
description
<< "Default file manager changed (old: "
<< fDefaultFileManager->GetFileType()
<< ", new:" << fileManager->GetFileType() << ")";
G4Exception("G4GenericFileManager::OpenFile",
"Analysis_W001", JustWarning, description);
}
fDefaultFileManager = fileManager;
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("open", "analysis file", fileName);
}
#endif
auto finalResult = true;
auto result = true;
// Save the default file name
// both in the generic file manager and the output specific one
result = SetFileName(fileName);
finalResult = finalResult && result;
result = fDefaultFileManager->SetFileName(fileName);
finalResult = finalResult && result;
result = fDefaultFileManager->OpenFile(fileName);
finalResult = finalResult && result;
#ifdef G4VERBOSE
if ( fState.GetVerboseL1() ) {
fState.GetVerboseL1()->Message("open", "analysis file", fileName, finalResult);
}
#endif
fLockDirectoryNames = true;
fIsOpenFile = true;
return finalResult;
}
//_____________________________________________________________________________
G4bool G4GenericFileManager::OpenFiles()
{
// Open all files regeistered with objects
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("open", "analysis files", "");
}
#endif
auto finalResult = true;
auto result = true;
// process names registered in base file manager
for ( auto fileName : GetFileNames() ) {
auto fileManager = GetFileManager(fileName);
if ( ! fileManager ) {
FileManagerException(fileName, "OpenFiles", "W001", fHdf5Warn);
continue;
}
result = fileManager->CreateFile(fileName);
finalResult = result && finalResult;
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL3() ) {
fState.GetVerboseL3()->Message("open", "analysis files", "", finalResult);
}
#endif
return finalResult;
}
//_____________________________________________________________________________
G4bool G4GenericFileManager::WriteFiles()
{
// Finish write for all files regeistered with objects
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("write", "files", "");
}
#endif
auto finalResult = true;
auto result = true;
for ( auto fileManager : fFileManagers ) {
if ( ! fileManager ) continue;
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("write", fileManager->GetFileType(), "files");
}
#endif
result = fileManager->WriteFiles();
finalResult = result && finalResult;
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL3() ) {
fState.GetVerboseL3()->Message("write", "files", "", finalResult);
}
#endif
return finalResult;
}
//_____________________________________________________________________________
G4bool G4GenericFileManager::CloseFiles()
{
// Close all files regeistered with objects
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("close", "files", "");
}
#endif
auto finalResult = true;
auto result = true;
for ( auto fileManager : fFileManagers ) {
if ( ! fileManager ) continue;
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("close", fileManager->GetFileType(), "files");
}
#endif
result = fileManager->CloseFiles();
finalResult = result && finalResult;
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL3() ) {
fState.GetVerboseL3()->Message("close", "files", "", finalResult);
}
#endif
return finalResult;
}
//_____________________________________________________________________________
G4bool G4GenericFileManager::DeleteEmptyFiles()
{
// Close all files regeistered with objects
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("delete", "empty files", "");
}
#endif
auto finalResult = true;
auto result = true;
for ( auto fileManager : fFileManagers ) {
if ( ! fileManager ) continue;
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("delete", fileManager->GetFileType(), "files");
}
#endif
result = fileManager->DeleteEmptyFiles();
finalResult = result && finalResult;
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL3() ) {
fState.GetVerboseL3()->Message("delete", "empty files", "", finalResult);
}
#endif
return finalResult;
}
//_____________________________________________________________________________
G4bool G4GenericFileManager::CreateFile(const G4String& fileName)
{
// New prototype, fully implemented in templated base class
auto fileManager = GetFileManager(fileName);
if ( ! fileManager ) {
FileManagerException(fileName, "CreateFile", "W001", fHdf5Warn);
return false;
}
return fileManager->CreateFile(fileName);
}
//_____________________________________________________________________________
G4bool G4GenericFileManager::WriteFile(const G4String& fileName)
{
// New prototype, fully implemented in templated base class
auto fileManager = GetFileManager(fileName);
if ( ! fileManager ) {
FileManagerException(fileName, "WriteFile", "W021", fHdf5Warn);
return false;
}
return fileManager->WriteFile(fileName);
}
//_____________________________________________________________________________
G4bool G4GenericFileManager::CloseFile(const G4String& fileName)
{
// New prototype, fully implemented in templated base class
auto fileManager = GetFileManager(fileName);
if ( ! fileManager ) {
FileManagerException(fileName, "CloseFile", "W021", fHdf5Warn);
return false;
}
return fileManager->CloseFile(fileName);
}
//_____________________________________________________________________________
G4bool G4GenericFileManager::SetIsEmpty(const G4String& fileName, G4bool isEmpty)
{
auto fileManager = GetFileManager(fileName);
if ( ! fileManager ) {
FileManagerException(fileName, "SetIsEmpty", "W021", fHdf5Warn);
return false;
}
return fileManager->SetIsEmpty(fileName, isEmpty);
}
//_____________________________________________________________________________
void G4GenericFileManager::SetDefaultFileType(const G4String& value)
{
// Check if value correspond to a valid file type
auto output = G4Analysis::GetOutput(value);
if ( output == G4AnalysisOutput::kNone ) {
G4ExceptionDescription description;
description
<< "The file type " << value << "is not supported." << G4endl
<< "The default type " << fDefaultFileType << " will be used.";
G4Exception("G4GenericFileManager::SetDeafultFileType",
"Analysis_W051", JustWarning, description);
return;
}
fDefaultFileType = value;
}
//_____________________________________________________________________________
std::shared_ptr<G4VNtupleFileManager>
G4GenericFileManager::CreateNtupleFileManager(G4AnalysisOutput output)
{
if ( ! GetFileManager(output) ) {
CreateFileManager(output);
}
std::shared_ptr<G4VNtupleFileManager> vNtupleFileManager = nullptr;
G4String failure;
switch ( output ) {
case G4AnalysisOutput::kCsv: {
auto ntupleFileManager = std::make_shared<G4CsvNtupleFileManager>(fState);
ntupleFileManager->SetFileManager(fCsvFileManager);
vNtupleFileManager = ntupleFileManager;
break;
}
case G4AnalysisOutput::kHdf5: {
#ifdef TOOLS_USE_HDF5
auto ntupleFileManager = std::make_shared<G4Hdf5NtupleFileManager>(fState);
ntupleFileManager->SetFileManager(fHdf5FileManager);
vNtupleFileManager = ntupleFileManager;
#else
failure = " Hdf5 is not available";
#endif
break;
}
case G4AnalysisOutput::kRoot: {
auto ntupleFileManager = std::make_shared<G4RootNtupleFileManager>(fState);
ntupleFileManager->SetFileManager(fRootFileManager);
vNtupleFileManager = ntupleFileManager;
break;
}
case G4AnalysisOutput::kXml: {
auto ntupleFileManager = std::make_shared<G4XmlNtupleFileManager>(fState);
ntupleFileManager->SetFileManager(fXmlFileManager);
vNtupleFileManager = ntupleFileManager;
break;
}
case G4AnalysisOutput::kNone:
break;
}
if ( ! vNtupleFileManager ) {
G4ExceptionDescription description;
description
<< " "
<< "Failed to create ntuple file manager of " << G4Analysis::GetOutputName(output) << " type."
<< failure;
G4Exception("G4GenericFileManager::CreateNtupleFileManager",
"Analysis_W002", JustWarning, description);
}
return vNtupleFileManager;
}