Import Geant4 11.0.0 source tree

This commit is contained in:
Gabriele Cosmo
2021-12-10 14:46:44 +01:00
committed by Ben Morgan
parent 6399a014b6
commit 80e2389dd8
3932 changed files with 202519 additions and 246221 deletions
+78 -299
View File
@@ -29,231 +29,51 @@
#include "G4RootAnalysisManager.hh"
#include "G4RootFileManager.hh"
#include "G4RootNtupleFileManager.hh"
#include "G4AnalysisVerbose.hh"
#include "G4AnalysisManagerState.hh"
#include "G4AnalysisUtilities.hh"
#include "G4ThreadLocalSingleton.hh"
#include "G4Threading.hh"
#include "G4AutoLock.hh"
#include <iostream>
#include <cstdio>
using namespace G4Analysis;
// mutex in a file scope
namespace {
//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;
}
G4RootAnalysisManager* G4RootAnalysisManager::fgMasterInstance = nullptr;
G4ThreadLocal G4RootAnalysisManager* G4RootAnalysisManager::fgInstance = nullptr;
//_____________________________________________________________________________
G4RootAnalysisManager* G4RootAnalysisManager::Instance()
{
if ( fgInstance == nullptr ) {
G4bool isMaster = ! G4Threading::IsWorkerThread();
fgInstance = new G4RootAnalysisManager(isMaster);
}
return fgInstance;
}
static G4ThreadLocalSingleton<G4RootAnalysisManager> instance;
fgIsInstance = true;
return instance.Instance();
}
//_____________________________________________________________________________
G4bool G4RootAnalysisManager::IsInstance()
{
return ( fgInstance != nullptr );
}
return fgIsInstance;
}
//_____________________________________________________________________________
G4RootAnalysisManager::G4RootAnalysisManager(G4bool isMaster)
: G4ToolsAnalysisManager("Root", isMaster),
fFileManager(nullptr),
fNtupleFileManager(nullptr)
G4RootAnalysisManager::G4RootAnalysisManager()
: G4ToolsAnalysisManager("Root")
{
if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
G4ExceptionDescription description;
description
<< " "
<< "G4RootAnalysisManager already exists."
<< "Cannot create another instance.";
G4Exception("G4RootAnalysisManager::G4RootAnalysisManager()",
"Analysis_F001", FatalException, description);
}
if ( isMaster ) fgMasterInstance = this;
fgInstance = this;
if ( ! G4Threading::IsWorkerThread() ) fgMasterInstance = this;
// File manager
fFileManager = std::make_shared<G4RootFileManager>(fState);
SetFileManager(fFileManager);
fFileManager->SetBasketSize(fgkDefaultBasketSize);
fFileManager->SetBasketEntries(fgkDefaultBasketEntries);
// Ntuple file manager
fNtupleFileManager = std::make_shared<G4RootNtupleFileManager>(fState);
fNtupleFileManager->SetFileManager(fFileManager);
fNtupleFileManager->SetBookingManager(fNtupleBookingManager);
}
//_____________________________________________________________________________
G4RootAnalysisManager::~G4RootAnalysisManager()
{
if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
fgInstance = nullptr;
fgIsInstance = false;
}
//
// private methods
//
//_____________________________________________________________________________
G4bool G4RootAnalysisManager::WriteH1()
{
auto h1Vector = fH1Manager->GetH1Vector();
auto hnVector = fH1Manager->GetHnVector();
if ( ! h1Vector.size() ) return true;
auto result = true;
if ( ! G4Threading::IsWorkerThread() ) {
result = WriteT(h1Vector, hnVector, "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 G4RootAnalysisManager::WriteH2()
{
auto h2Vector = fH2Manager->GetH2Vector();
auto hnVector = fH2Manager->GetHnVector();
if ( ! h2Vector.size() ) return true;
auto result = true;
if ( ! G4Threading::IsWorkerThread() ) {
result = WriteT(h2Vector, hnVector, "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 G4RootAnalysisManager::WriteH3()
{
auto h3Vector = fH3Manager->GetH3Vector();
auto hnVector = fH3Manager->GetHnVector();
if ( ! h3Vector.size() ) return true;
auto result = true;
if ( ! G4Threading::IsWorkerThread() ) {
result = WriteT(h3Vector, hnVector, "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 G4RootAnalysisManager::WriteP1()
{
auto p1Vector = fP1Manager->GetP1Vector();
auto hnVector = fP1Manager->GetHnVector();
if ( ! p1Vector.size() ) return true;
auto result = true;
if ( ! G4Threading::IsWorkerThread() ) {
result = WriteT(p1Vector, hnVector, "p1");
}
else {
// The worker manager just adds its histograms to the master
// This operation needs a lock
G4AutoLock lP1(&mergeP1Mutex);
fgMasterInstance->fP1Manager->AddP1Vector(p1Vector);
lP1.unlock();
}
return result;
}
//_____________________________________________________________________________
G4bool G4RootAnalysisManager::WriteP2()
{
auto p2Vector = fP2Manager->GetP2Vector();
auto hnVector = fP2Manager->GetHnVector();
if ( ! p2Vector.size() ) return true;
auto result = true;
if ( ! G4Threading::IsWorkerThread() ) {
result = WriteT(p2Vector, hnVector, "p2");
}
else {
// The worker manager just adds its histograms to the master
// This operation needs a lock
G4AutoLock lP2(&mergeP2Mutex);
fgMasterInstance->fP2Manager->AddP2Vector(p2Vector);
lP2.unlock();
}
return result;
}
//_____________________________________________________________________________
G4bool G4RootAnalysisManager::Reset()
{
// Reset histograms and ntuple
auto finalResult = true;
auto result = G4ToolsAnalysisManager::Reset();
finalResult = finalResult && result;
result = fNtupleFileManager->Reset();
finalResult = result && finalResult;
return finalResult;
}
//
// protected methods
//
@@ -264,159 +84,118 @@ G4bool G4RootAnalysisManager::OpenFileImpl(const G4String& fileName)
// and set it to base class which takes then their ownership
SetNtupleManager(fNtupleFileManager->CreateNtupleManager());
auto finalResult = true;
auto result = true;
// Open file
if ( fNtupleFileManager->GetMergeMode() != G4NtupleMergeMode::kSlave ) {
auto result = fFileManager->OpenFile(fileName);
finalResult = finalResult && result;
result &= fFileManager->OpenFile(fileName);
}
// Open ntuple file(s) and create ntuples from bookings
auto result = fNtupleFileManager->ActionAtOpenFile(fFileManager->GetFullFileName());
finalResult = finalResult && result;
result &= fNtupleFileManager->ActionAtOpenFile(fFileManager->GetFullFileName());
return finalResult;
}
return result;
}
//_____________________________________________________________________________
G4bool G4RootAnalysisManager::WriteImpl()
G4bool G4RootAnalysisManager::WriteImpl()
{
auto finalResult = true;
auto result = true;
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("write", "files", "");
#endif
Message(kVL4, "write", "files");
if ( ! fgMasterInstance &&
( ( ! fH1Manager->IsEmpty() ) || ( ! fH2Manager->IsEmpty() ) ||
( ! fH3Manager->IsEmpty() ) || ( ! fP1Manager->IsEmpty() ) ||
( ! fP2Manager->IsEmpty() ) ) ) {
G4ExceptionDescription description;
description
<< " " << "No master G4RootAnalysisManager instance exists."
<< G4endl
<< " " << "Histogram/profile data will not be merged.";
G4Exception("G4RootAnalysisManager::Write()",
"Analysis_W031", JustWarning, description);
if ( G4Threading::IsWorkerThread() ) {
result &= G4ToolsAnalysisManager::Merge();
}
// H1
auto result = WriteH1();
finalResult = finalResult && result;
else {
// Open all files registered with objects
fFileManager->OpenFiles();
// H2
result = WriteH2();
finalResult = finalResult && result;
// H3
result = WriteH3();
finalResult = finalResult && result;
// P1
result = WriteP1();
finalResult = finalResult && result;
// P2
result = WriteP2();
finalResult = finalResult && result;
// Write all histograms/profile on master
result &= G4ToolsAnalysisManager::WriteImpl();
}
// Ntuples
result = fNtupleFileManager->ActionAtWrite();
finalResult = finalResult && result;
result &= fNtupleFileManager->ActionAtWrite();
// File
if ( fNtupleFileManager->GetMergeMode() != G4NtupleMergeMode::kSlave ) {
// write all open files
result = fFileManager->WriteFiles();
finalResult = finalResult && result;
result &= fFileManager->WriteFiles();
}
// Write ASCII if activated
if ( IsAscii() ) {
result = WriteAscii(fFileManager->GetFileName());
finalResult = finalResult && result;
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL2() ) {
fState.GetVerboseL2()->Message("write", "files", "", finalResult);
result &= WriteAscii(fFileManager->GetFileName());
}
#endif
return finalResult;
Message(kVL3, "write", "files", "", result);
return result;
}
//_____________________________________________________________________________
G4bool G4RootAnalysisManager::CloseFileImpl(G4bool reset)
{
auto finalResult = true;
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("close", "files", "");
#endif
auto result = true;
if ( reset ) {
result = Reset();
if ( ! result ) {
G4ExceptionDescription description;
description << " " << "Resetting data failed";
G4Exception("G4RootAnalysisManager::CloseFile()",
"Analysis_W021", JustWarning, description);
}
}
finalResult = finalResult && result;
result = fNtupleFileManager->ActionAtCloseFile(reset);
finalResult = finalResult && result;
Message(kVL4, "close", "files");
if ( reset ) {
if ( ! Reset() ) {
Warn("Resetting data failed", fkClass, "CloseFileImpl");
result = false;
}
}
result &= fNtupleFileManager->ActionAtCloseFile(reset);
if ( fNtupleFileManager->GetMergeMode() != G4NtupleMergeMode::kSlave ) {
// close all open files
result = fFileManager->CloseFiles();
finalResult = finalResult && result;
result &= fFileManager->CloseFiles();
}
// No files clean-up in sequential mode
if ( ! G4Threading::IsMultithreadedApplication() ) return finalResult;
if ( ! G4Threading::IsMultithreadedApplication() ) return result;
G4bool isNtupleManagerEmpty = fNtupleBookingManager->IsEmpty();
// Delete files if empty in MT mode
if ( ( fState.GetIsMaster() &&
fH1Manager->IsEmpty() && fH2Manager->IsEmpty() && fH3Manager->IsEmpty() &&
fP1Manager->IsEmpty() && fP2Manager->IsEmpty() && isNtupleManagerEmpty ) ||
( ( ! fState.GetIsMaster() ) && isNtupleManagerEmpty &&
fNtupleFileManager->GetMergeMode() == G4NtupleMergeMode::kNone ) ) {
if ( ( fState.GetIsMaster() && G4ToolsAnalysisManager::IsEmpty() &&
isNtupleManagerEmpty ) ||
( (! fState.GetIsMaster()) && isNtupleManagerEmpty &&
fNtupleFileManager->GetMergeMode() == G4NtupleMergeMode::kNone ) ) {
result = ! std::remove(fFileManager->GetFullFileName());
// std::remove returns 0 when success
if ( ! result ) {
G4ExceptionDescription description;
description << " " << "Removing file "
<< fFileManager->GetFullFileName() << " failed";
G4Exception("G4RootAnalysisManager::CloseFile()",
"Analysis_W021", JustWarning, description);
}
finalResult = finalResult && result;
#ifdef G4VERBOSE
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()
->Message("delete", "empty file", fFileManager->GetFullFileName());
#endif
if ( std::remove(fFileManager->GetFullFileName()) ) {
// std::remove returns 0 when success
Warn( "Removing file " + fFileManager->GetFullFileName() + " failed",
fkClass, "CloseFileImpl");
result = false;
}
Message(kVL1, "delete", "empty file", fFileManager->GetFullFileName());
}
else {
#ifdef G4VERBOSE
if ( fState.GetVerboseL2() )
fState.GetVerboseL2()
->Message("close", "files", "");
#endif
Message(kVL3, "close", "files");
}
return finalResult;
return result;
}
//_____________________________________________________________________________
G4bool G4RootAnalysisManager::ResetImpl()
{
// Reset histograms and ntuple
auto result = true;
result &= G4ToolsAnalysisManager::ResetImpl();
if ( fNtupleFileManager != nullptr ) {
result &= fNtupleFileManager->Reset();
}
return result;
}
//
@@ -424,20 +203,20 @@ G4bool G4RootAnalysisManager::CloseFileImpl(G4bool reset)
//
//_____________________________________________________________________________
void G4RootAnalysisManager::SetNtupleMerging(G4bool mergeNtuples,
void G4RootAnalysisManager::SetNtupleMerging(G4bool mergeNtuples,
G4int nofNtupleFiles)
{
fNtupleFileManager->SetNtupleMerging(mergeNtuples, nofNtupleFiles);
}
//_____________________________________________________________________________
void G4RootAnalysisManager::SetNtupleRowWise(G4bool rowWise, G4bool rowMode)
void G4RootAnalysisManager::SetNtupleRowWise(G4bool rowWise, G4bool rowMode)
{
fNtupleFileManager->SetNtupleRowWise(rowWise, rowMode);
}
//_____________________________________________________________________________
void G4RootAnalysisManager::SetBasketSize(unsigned int basketSize)
void G4RootAnalysisManager::SetBasketSize(unsigned int basketSize)
{
fFileManager->SetBasketSize(basketSize);
}
+29 -363
View File
@@ -29,58 +29,30 @@
#include "G4RootAnalysisReader.hh"
#include "G4RootRFileManager.hh"
#include "G4RootRNtupleManager.hh"
#include "G4AnalysisVerbose.hh"
#include "G4AnalysisUtilities.hh"
#include "G4ThreadLocalSingleton.hh"
#include "G4Threading.hh"
#include <tools/rroot/file>
#include <tools/rroot/streamers>
#include <tools/rroot/fac>
#include <tools/rroot/tree>
#include <tools/rroot/ntuple>
#include <iostream>
#include <cstdio>
using namespace G4Analysis;
G4RootAnalysisReader* G4RootAnalysisReader::fgMasterInstance = nullptr;
G4ThreadLocal G4RootAnalysisReader* G4RootAnalysisReader::fgInstance = nullptr;
//_____________________________________________________________________________
G4RootAnalysisReader* G4RootAnalysisReader::Instance()
{
if ( fgInstance == nullptr ) {
G4bool isMaster = ! G4Threading::IsWorkerThread();
fgInstance = new G4RootAnalysisReader(isMaster);
}
return fgInstance;
}
static G4ThreadLocalSingleton<G4RootAnalysisReader> instance;
return instance.Instance();
}
//_____________________________________________________________________________
G4RootAnalysisReader::G4RootAnalysisReader(G4bool isMaster)
: G4ToolsAnalysisReader("Root", isMaster),
fNtupleManager(nullptr),
fFileManager(nullptr)
G4RootAnalysisReader::G4RootAnalysisReader()
: G4ToolsAnalysisReader("Root")
{
if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
G4ExceptionDescription description;
description
<< " "
<< "G4RootAnalysisReader already exists."
<< "Cannot create another instance.";
G4Exception("G4RootAnalysisReader::G4RootAnalysisReader()",
"Analysis_F001", FatalException, description);
}
if ( isMaster ) fgMasterInstance = this;
fgInstance = this;
if ( ! G4Threading::IsWorkerThread() ) fgMasterInstance = this;
// Create managers
fNtupleManager = new G4RootRNtupleManager(fState);
fFileManager = new G4RootRFileManager(fState);
// The managers will be deleted by the base class
fNtupleManager = std::make_shared<G4RootRNtupleManager>(fState);
fFileManager = std::make_shared<G4RootRFileManager>(fState);
fNtupleManager->SetFileManager(fFileManager);
// Set managers to base class
SetNtupleManager(fNtupleManager);
SetFileManager(fFileManager);
@@ -90,349 +62,43 @@ G4RootAnalysisReader::G4RootAnalysisReader(G4bool isMaster)
G4RootAnalysisReader::~G4RootAnalysisReader()
{
if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
fgInstance = nullptr;
}
//
//
// private methods
//
//_____________________________________________________________________________
tools::rroot::buffer* G4RootAnalysisReader::GetBuffer(
const G4String& fileName,
const G4String& objectName,
const G4String& inFunction)
{
// Get buffer for reading histogram or profile specified by objectNmae
// for a file specified by fileName;
// open the file if it was not yet open
// Histograms and profiles are not saved per thread
G4bool isPerThread = false;
// Get or open a file
auto rfile = fFileManager->GetRFile(fileName, isPerThread);
if ( ! rfile ) {
if ( ! fFileManager->OpenRFile(fileName, isPerThread) ) return nullptr;
rfile = fFileManager->GetRFile(fileName, isPerThread);
}
auto key
= ( ! rfile ) ? nullptr : rfile->dir().find_key(objectName);
unsigned int size;
//char* charBuffer
// = ( ! key ) ? 0 : key->get_object_buffer(size);
char* charBuffer = 0;
if ( key ) charBuffer = key->get_object_buffer(*rfile, size);
if ( ! charBuffer ) {
G4ExceptionDescription description;
description
<< " "
<< "Cannot get " << objectName << " in file " << fileName;
G4Exception(inFunction, "Analysis_WR011", JustWarning, description);
return nullptr;
}
auto verbose = false;
return new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer,
key->key_length(), verbose);
}
//_____________________________________________________________________________
G4bool G4RootAnalysisReader::Reset()
{
// Reset histograms and ntuple
auto finalResult = true;
auto result = G4ToolsAnalysisReader::Reset();
finalResult = finalResult && result;
auto result = true;
result = fNtupleManager->Reset();
finalResult = finalResult && result;
return finalResult;
}
//
result &= G4ToolsAnalysisReader::Reset();
result &= fNtupleManager->Reset();
return result;
}
//
// protected methods
//
//_____________________________________________________________________________
G4int G4RootAnalysisReader::ReadH1Impl(const G4String& h1Name,
const G4String& fileName,
const G4String& /*dirName*/,
G4bool /*isUserFileName*/)
G4bool G4RootAnalysisReader::CloseFilesImpl(G4bool reset)
{
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("read", "h1", h1Name);
#endif
Message(kVL4, "close", "files");
auto buffer = GetBuffer(fileName, h1Name, "ReadH1Impl");
if ( ! buffer ) return kInvalidId;
auto h1 = tools::rroot::TH1D_stream(*buffer);
delete buffer;
if ( ! h1 ) {
G4ExceptionDescription description;
description
<< " "
<< "Streaming " << h1Name << " in file " << fileName << " failed.";
G4Exception("G4RootAnalysisReader::ReadH1Impl",
"Analysis_WR011", JustWarning, description);
return kInvalidId;
}
auto id = fH1Manager->AddH1(h1Name, h1);
auto result = true;
#ifdef G4VERBOSE
if ( fState.GetVerboseL2() )
fState.GetVerboseL2()->Message("read", "h1", h1Name, id > kInvalidId);
#endif
return id;
}
//_____________________________________________________________________________
G4int G4RootAnalysisReader::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 buffer = GetBuffer(fileName, h2Name, "ReadH2Impl");
if ( ! buffer ) return kInvalidId;
// if h2Name represents H1, then we get !!
// tools::rroot::buffer::check_byte_count : object of class "TNamed" read too few bytes (603979762 missing).
// tools::rroot::buffer::check_byte_count : "TNamed" streamer not in sync with data on file, fix streamer.
// Segmentation fault (core dumped)
auto h2 = tools::rroot::TH2D_stream(*buffer);
delete buffer;
if ( ! h2 ) {
G4ExceptionDescription description;
description
<< " "
<< "Streaming " << h2Name << " in file " << fileName << " failed.";
G4Exception("G4RootAnalysisReader::ReadH2Impl",
"Analysis_WR011", JustWarning, description);
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 G4RootAnalysisReader::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 buffer = GetBuffer(fileName, h3Name, "ReadH3Impl");
if ( ! buffer ) return kInvalidId;
auto h3 = tools::rroot::TH3D_stream(*buffer);
delete buffer;
if ( ! h3 ) {
G4ExceptionDescription description;
description
<< " "
<< "Streaming " << h3Name << " in file " << fileName << " failed.";
G4Exception("G4RootAnalysisReader::ReadH3Impl",
"Analysis_WR011", JustWarning, description);
return kInvalidId;
}
auto id = fH3Manager->AddH3(h3Name, h3);
#ifdef G4VERBOSE
if ( fState.GetVerboseL2() )
fState.GetVerboseL2()->Message("read", "h3", h3Name, id > kInvalidId);
#endif
return id;
/*
// not yet available
return kInvalidId;
*/
}
//_____________________________________________________________________________
G4int G4RootAnalysisReader::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 buffer = GetBuffer(fileName, p1Name, "ReadP1Impl");
if ( ! buffer ) return kInvalidId;
auto p1 = tools::rroot::TProfile_stream(*buffer);
delete buffer;
if ( ! p1 ) {
G4ExceptionDescription description;
description
<< " "
<< "Streaming " << p1Name << " in file " << fileName << " failed.";
G4Exception("G4RootAnalysisReader::ReadP1Impl",
"Analysis_WR011", JustWarning, description);
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 G4RootAnalysisReader::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 buffer = GetBuffer(fileName, p2Name, "ReadP2Impl");
if ( ! buffer ) return kInvalidId;
auto p2 = tools::rroot::TProfile2D_stream(*buffer);
delete buffer;
if ( ! p2 ) {
G4ExceptionDescription description;
description
<< " "
<< "Streaming " << p2Name << " in file " << fileName << " failed.";
G4Exception("G4RootAnalysisReader::ReadP2Impl",
"Analysis_WR011", JustWarning, description);
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 G4RootAnalysisReader::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 per thread
// but do not apply the thread suffix if fileName is provided explicitly
auto isPerThread = true;
if ( isUserFileName ) isPerThread = false;
// Get or open a file
auto rfile = fFileManager->GetRFile(fileName, isPerThread);
if ( ! rfile ) {
if ( ! fFileManager->OpenRFile(fileName, isPerThread) ) return kInvalidId;
rfile = fFileManager->GetRFile(fileName, isPerThread);
}
auto key = rfile->dir().find_key(ntupleName);
if ( ! key ) {
G4ExceptionDescription description;
description
<< " "
<< "Key " << ntupleName << " for Ntuple not found in file " << fileName;
G4Exception("G4RootAnalysisReader::ReadNtupleImpl()",
"Analysis_WR011", JustWarning, description);
return kInvalidId;
if (reset) {
result &= Reset();
}
unsigned int size;
char* charBuffer = key->get_object_buffer(*rfile, size);
if ( ! charBuffer ) {
G4ExceptionDescription description;
description
<< " "
<< "Cannot get data buffer for Ntuple " << ntupleName << " in file " << fileName;
G4Exception("G4RootAnalysisReader::ReadNtupleImpl()",
"Analysis_WR021", JustWarning, description);
return kInvalidId;
}
auto verbose = false;
auto buffer
= new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer,
key->key_length(), verbose);
buffer->set_map_objs(true);
fFileManager->CloseFiles();
auto fac = new tools::rroot::fac(G4cout);
Message(kVL2, "close", "files", "", result);
auto tree = new tools::rroot::tree(*rfile, *fac);
if ( ! tree->stream(*buffer) ) {
G4ExceptionDescription description;
description
<< " "
<< "TTree streaming failed for Ntuple " << ntupleName << " in file " << fileName;
G4Exception("G4RootAnalysisReader::ReadNtupleImpl()",
"Analysis_WR021", JustWarning, description);
delete buffer;
delete tree;
return kInvalidId;
}
auto rntuple = new tools::rroot::ntuple(*tree); //use the flat ntuple API.
auto rntupleDescription = new G4TRNtupleDescription<tools::rroot::ntuple>(rntuple);
auto id = fNtupleManager->SetNtuple(rntupleDescription);
#ifdef G4VERBOSE
if ( fState.GetVerboseL2() )
fState.GetVerboseL2()->Message("read", "ntuple", ntupleName, id > kInvalidId);
#endif
return id;
}
return result;
}
+31 -65
View File
@@ -35,17 +35,12 @@
#include "tools/wroot/to"
#include "tools/zlib"
#include <iostream>
#include <cstdio>
using namespace tools;
using namespace G4Analysis;
//_____________________________________________________________________________
G4RootFileManager::G4RootFileManager(const G4AnalysisManagerState& state)
: G4VTFileManager<G4RootFile>(state),
fBasketSize(0),
fBasketEntries(0)
: G4VTFileManager<G4RootFile>(state)
{
// Create helpers defined in the base class
fH1FileManager = std::make_shared<G4RootHnFileManager<histo::h1d>>(this);
@@ -55,52 +50,33 @@ G4RootFileManager::G4RootFileManager(const G4AnalysisManagerState& state)
fP2FileManager = std::make_shared<G4RootHnFileManager<histo::p2d>>(this);
}
//_____________________________________________________________________________
G4RootFileManager::~G4RootFileManager()
{}
//
// private methods
//
//_____________________________________________________________________________
tools::wroot::directory* G4RootFileManager::CreateDirectory(
std::shared_ptr<tools::wroot::file> rfile,
#ifdef G4VERBOSE
const G4String& directoryName, const G4String& objectType) const
#else
const G4String& directoryName, const G4String& /*objectType*/) const
#endif
tools::wroot::file* rfile,
const G4String& directoryName, [[maybe_unused]] const G4String& objectType) const
{
if ( ! rfile ) return nullptr;
if ( directoryName == "" ) {
// Do not create a new directory if its name is not set
return &(rfile->dir());
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()
->Message("create", "directory for " + objectType, directoryName);
#endif
}
Message(kVL4, "create", "directory for " + objectType, directoryName);
auto directory = rfile->dir().mkdir(directoryName);
if ( ! directory ) {
G4ExceptionDescription description;
description << " "
<< "cannot create directory " << directoryName;
G4Exception("G4RootFileManager::CreateDirectory()",
"Analysis_W001", JustWarning, description);
return nullptr;
}
#ifdef G4VERBOSE
Warn("Cannot create directory " + directoryName, fkClass, "CreateDirectory");
return nullptr;
}
else {
if ( fState.GetVerboseL2() )
fState.GetVerboseL2()
->Message("create", "directory for " + objectType, directoryName);
}
#endif
Message(kVL2, "create", "directory for " + objectType, directoryName);
}
return directory;
}
@@ -125,49 +101,46 @@ G4String G4RootFileManager::GetNtupleFileName(
// update filename per mainNumber
if ( mainNumber > -1 ) {
// update filename per mainNumber
ntupleFileName
ntupleFileName
= G4Analysis::GetNtupleFileName(ntupleFileName, GetFileType(), mainNumber);
}
return ntupleFileName;
}
}
//
// protected methods
//
//_____________________________________________________________________________
std::shared_ptr<G4RootFile>
std::shared_ptr<G4RootFile>
G4RootFileManager::CreateFileImpl(const G4String& fileName)
{
// create file
std::shared_ptr<wroot::file> file = std::make_shared<wroot::file>(G4cout, fileName);
file->add_ziper('Z',compress_buffer);
file->set_compression(fState.GetCompressionLevel());
if ( ! file->is_open() ) {
G4ExceptionDescription description;
description << " " << "Cannot create file " << fileName;
G4Exception("G4RootFileManager::CreateFileImpl()",
"Analysis_W001", JustWarning, description);
Warn("Cannot create file " + fileName, fkClass, "CreateFileImpl");
return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr);
}
// create histo directory
tools::wroot::directory* hdirectory
= CreateDirectory(file, fHistoDirectoryName, "histograms");
tools::wroot::directory* hdirectory
= CreateDirectory(file.get(), fHistoDirectoryName, "histograms");
if ( ! hdirectory ) {
// Warning is issued in CreateDirectory
return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr);
}
}
// create ntuple directory
tools::wroot::directory* ndirectory
= CreateDirectory(file, fNtupleDirectoryName, "ntuples");
tools::wroot::directory* ndirectory
= CreateDirectory(file.get(), fNtupleDirectoryName, "ntuples");
if ( ! ndirectory ) {
// Warning is issued in CreateDirectory
return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr);
}
}
return std::make_shared<G4RootFile>(file, hdirectory, ndirectory);
}
@@ -184,7 +157,7 @@ G4bool G4RootFileManager::WriteFileImpl(std::shared_ptr<G4RootFile> file)
}
//_____________________________________________________________________________
G4bool G4RootFileManager::CloseFileImpl(std::shared_ptr<G4RootFile> file)
G4bool G4RootFileManager::CloseFileImpl(std::shared_ptr<G4RootFile> file)
{
// New prototype: called by G4TFileManager base classe
@@ -209,25 +182,19 @@ G4bool G4RootFileManager::OpenFile(const G4String& fileName)
auto name = GetFullFileName();
if ( fFile ) {
G4ExceptionDescription description;
description << "File " << fileName << " already exists.";
G4Exception("G4RootFileManager::OpenFile()",
"Analysis_W001", JustWarning, description);
Warn("File " + fileName + " already exists.", fkClass, "OpenFile");
fFile.reset();
}
// Create file (and save in in the file map)
fFile = CreateTFile(name);
if ( ! fFile ) {
G4ExceptionDescription description;
description << "Failed to create file " << fileName;
G4Exception("G4RootFileManager::OpenFile()",
"Analysis_W001", JustWarning, description);
Warn("Failed to create file " + fileName, fkClass, "OpenFile");
return false;
}
LockDirectoryNames();
fIsOpenFile = true;
fLockDirectoryNames = true;
return true;
}
@@ -268,7 +235,7 @@ std::shared_ptr<G4RootFile> G4RootFileManager::GetNtupleFile(
G4bool G4RootFileManager::CloseNtupleFile(
RootNtupleDescription* ntupleDescription)
{
auto finalResult = true;
auto result = true;
if ( ntupleDescription->fFile ) {
// Ntuple files are registered in file manager map.
@@ -276,10 +243,9 @@ G4bool G4RootFileManager::CloseNtupleFile(
ntupleDescription->fFile.reset();
// Notify not empty file
auto ntupleFileName = GetNtupleFileName(ntupleDescription);
auto result = SetIsEmpty(ntupleFileName, ! ntupleDescription->fHasFill);
finalResult = result && finalResult;
result &= SetIsEmpty(ntupleFileName, ! ntupleDescription->fHasFill);
}
return finalResult;
return result;
}
@@ -34,29 +34,22 @@
#include "tools/wroot/file"
#include "tools/wroot/ntuple"
using namespace G4Analysis;
//_____________________________________________________________________________
G4RootMainNtupleManager::G4RootMainNtupleManager(
G4RootNtupleManager* ntupleBuilder,
G4NtupleBookingManager* bookingManager,
std::shared_ptr<G4NtupleBookingManager> bookingManager,
G4bool rowWise,
G4int fileNumber,
const G4AnalysisManagerState& state)
: G4BaseAnalysisManager(state),
fNtupleBuilder(ntupleBuilder),
fBookingManager(bookingManager),
fFileManager(nullptr),
fBookingManager(std::move(bookingManager)),
fRowWise(rowWise),
fFileNumber(fileNumber),
fNtupleVector(),
fNtupleDescriptionVector()
fFileNumber(fileNumber)
{}
//_____________________________________________________________________________
G4RootMainNtupleManager::~G4RootMainNtupleManager()
{
// ntuple objects are deleted automatically when closing a file
}
//
// protected functions
//
@@ -71,12 +64,9 @@ void G4RootMainNtupleManager::CreateNtuple(RootNtupleDescription* ntupleDescript
auto ntupleFile = fFileManager->CreateNtupleFile(ntupleDescription, fFileNumber);
if ( ! ntupleFile ) {
if ( warn ) {
G4ExceptionDescription description;
description
<< "Ntuple file must be defined first." << G4endl
<< "Cannot create main ntuple.";
G4Exception("G4RootMainAnalysisManager::CreateNtuple",
"Analysis_W002", JustWarning, description);
Warn("Ntuple file must be defined first.\n"
"Cannot create main ntuple.",
fkClass, "CreateNtuple");
}
return;
}
@@ -84,11 +74,7 @@ void G4RootMainNtupleManager::CreateNtuple(RootNtupleDescription* ntupleDescript
// Get ntuple booking
auto ntupleBooking = ntupleDescription->fNtupleBooking;
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()
->Message("create", "main ntuple", ntupleBooking.name());
#endif
Message(kVL4, "create", "main ntuple", ntupleBooking.name());
// Create ntuple
auto ntuple = new tools::wroot::ntuple(*std::get<2>(*ntupleFile), ntupleBooking, fRowWise);
@@ -99,11 +85,7 @@ void G4RootMainNtupleManager::CreateNtuple(RootNtupleDescription* ntupleDescript
fNtupleVector.push_back(ntuple);
fNtupleDescriptionVector.push_back(ntupleDescription);
#ifdef G4VERBOSE
if ( fState.GetVerboseL3() )
fState.GetVerboseL3()
->Message("create", "main ntuple", ntupleBooking.name());
#endif
Message(kVL3, "create", "main ntuple", ntupleBooking.name());
}
//_____________________________________________________________________________
@@ -126,24 +108,29 @@ G4bool G4RootMainNtupleManager::Merge()
}
//_____________________________________________________________________________
G4bool G4RootMainNtupleManager::Reset(G4bool deleteNtuple)
G4bool G4RootMainNtupleManager::Reset()
{
for ( auto ntuple : fNtupleVector ) {
if ( deleteNtuple ) {
delete ntuple;
}
}
// ntuple object is deleted automatically when closing a file
fNtupleVector.clear();
fNtupleDescriptionVector.clear();
fNtupleVector.clear();
fNtupleDescriptionVector.clear();
return true;
}
//_____________________________________________________________________________
std::shared_ptr<G4RootFile>
void G4RootMainNtupleManager::ClearData()
{
// Reset function clears all data
Reset();
Message(G4Analysis::kVL2, "clear", "main ntuples");
}
//_____________________________________________________________________________
std::shared_ptr<G4RootFile>
G4RootMainNtupleManager::GetNtupleFile(RootNtupleDescription* ntupleDescription) const
{
{
auto perThread = false;
return fFileManager->GetNtupleFile(ntupleDescription, perThread, fFileNumber);
}
@@ -32,53 +32,24 @@
#include "G4RootMainNtupleManager.hh"
#include "G4RootPNtupleManager.hh"
#include "G4VAnalysisManager.hh"
#include "G4AnalysisVerbose.hh"
#include "G4AnalysisManagerState.hh"
#include "G4AnalysisUtilities.hh"
#include "G4Threading.hh"
#include "G4AutoLock.hh"
#include <iostream>
#include <cstdio>
namespace {
void MergingException(const G4String& functionName,
G4ExceptionDescription& description)
{
G4String inFunction = "G4RootNtupleFileManager::" + functionName;
G4Exception(inFunction, "Analysis_W013", JustWarning, description);
}
}
using namespace G4Analysis;
using std::make_shared;
using std::to_string;
G4RootNtupleFileManager* G4RootNtupleFileManager::fgMasterInstance = nullptr;
//_____________________________________________________________________________
G4RootNtupleFileManager::G4RootNtupleFileManager(const G4AnalysisManagerState& state)
: G4VNtupleFileManager(state, "root"),
fIsInitialized(false),
fNofNtupleFiles(0),
fNtupleRowWise(false),
fNtupleRowMode(true),
fNtupleMergeMode(G4NtupleMergeMode::kNone),
fNtupleManager(nullptr),
fSlaveNtupleManager(nullptr),
fFileManager(nullptr)
: G4VNtupleFileManager(state, "root")
{
if ( G4Threading::IsMasterThread() && fgMasterInstance ) {
G4ExceptionDescription description;
description
<< " "
<< "G4RootNtupleFileManager already exists."
<< "Cannot create another instance.";
G4Exception("G4RootNtupleFileManager::G4RootNtupleFileManager()",
"Analysis_F001", FatalException, description);
}
if ( G4Threading::IsMasterThread() ) fgMasterInstance = this;
// Do not merge ntuples by default
@@ -91,45 +62,37 @@ G4RootNtupleFileManager::G4RootNtupleFileManager(const G4AnalysisManagerState& s
//_____________________________________________________________________________
G4RootNtupleFileManager::~G4RootNtupleFileManager()
{
if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
}
//
//
// private methods
//
//_____________________________________________________________________________
void G4RootNtupleFileManager::SetNtupleMergingMode(G4bool mergeNtuples,
void G4RootNtupleFileManager::SetNtupleMergingMode(G4bool mergeNtuples,
G4int nofNtupleFiles)
{
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("set", "ntuple merging mode", "");
}
#endif
Message(kVL4, "set", "ntuple merging mode");
auto canMerge = true;
// Illegal situations
if ( mergeNtuples && ( ! G4Threading::IsMultithreadedApplication() ) ) {
G4ExceptionDescription description;
description
<< "Merging ntuples is not applicable in sequential application." << G4endl
<< "Setting was ignored.";
MergingException("SetNtupleMergingMode", description);
canMerge = false;
Warn("Merging ntuples is not applicable in sequential application.\n"
"Setting was ignored.",
fkClass, "SetNtupleMergingMode");
canMerge = false;
}
// Illegal situations
if ( mergeNtuples && G4Threading::IsMultithreadedApplication() &&
( ! fgMasterInstance ) ) {
G4ExceptionDescription description;
description
<< "Merging ntuples requires G4AnalysisManager instance on master." << G4endl
<< "Setting was ignored.";
MergingException("SetNtupleMergingMode", description);
canMerge = false;
Warn("Merging ntuples requires G4AnalysisManager instance on master.\n"
"Setting was ignored.",
fkClass, "SetNtupleMergingMode");
canMerge = false;
}
G4String mergingMode;
@@ -140,34 +103,28 @@ void G4RootNtupleFileManager::SetNtupleMergingMode(G4bool mergeNtuples,
else {
// Set the number of reduced ntuple files
fNofNtupleFiles = nofNtupleFiles;
// Check the number of reduced ntuple files
if ( fNofNtupleFiles < 0 ) {
G4ExceptionDescription description;
description
<< "Number of reduced files must be [0, nofThreads]." << G4endl
<< "Cannot set " << nofNtupleFiles << " files" << G4endl
<< "Ntuples will be merged in a single file.";
MergingException("SetNtupleMergingMode", description);
Warn("Number of reduced files must be [0, nofThreads].\n"
"Cannot set " + to_string(nofNtupleFiles) +" files.\n" +
"Setting was ignored.",
fkClass, "SetNtupleMergingMode");
fNofNtupleFiles = 0;
}
// Forced merging mode
G4bool isMaster = ! G4Threading::IsWorkerThread();
if ( isMaster ) {
fNtupleMergeMode = G4NtupleMergeMode::kMain;
mergingMode = "G4NtupleMergeMode::kMain";
} else {
fNtupleMergeMode = G4NtupleMergeMode::kSlave;
fNtupleMergeMode = G4NtupleMergeMode::kSlave;
mergingMode = "G4NtupleMergeMode::kSlave";
}
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL2() ) {
fState.GetVerboseL2()->Message("set", "ntuple merging mode", mergingMode);
}
#endif
Message(kVL2, "set", "ntuple merging mode", mergingMode);
}
//_____________________________________________________________________________
@@ -187,59 +144,53 @@ G4bool G4RootNtupleFileManager::CloseNtupleFiles()
{
// Close ntuple files
auto finalResult = true;
auto result = true;
auto ntupleVector = fNtupleManager->GetNtupleDescriptionVector();
for ( auto ntupleDescription : ntupleVector) {
auto result = fFileManager->CloseNtupleFile(ntupleDescription);
finalResult = finalResult && result;
result &= fFileManager->CloseNtupleFile(ntupleDescription);
}
return finalResult;
return result;
}
//
//
// public methods
//
//_____________________________________________________________________________
void G4RootNtupleFileManager::SetNtupleMerging(G4bool mergeNtuples,
void G4RootNtupleFileManager::SetNtupleMerging(G4bool mergeNtuples,
G4int nofNtupleFiles)
{
if ( fIsInitialized ) {
G4ExceptionDescription description;
description
<< "Cannot change merging mode." << G4endl
<< "The function must be called before OpenFile().";
MergingException("SetNtupleMerging", description);
return;
Warn("Cannot change merging mode.\n"
"The function must be called before OpenFile().",
fkClass, "SetNtupleMerging");
return;
}
// Set ntuple merging mode
// Set ntuple merging mode
SetNtupleMergingMode(mergeNtuples, nofNtupleFiles);
}
//_____________________________________________________________________________
void G4RootNtupleFileManager::SetNtupleRowWise(G4bool rowWise, G4bool rowMode)
void G4RootNtupleFileManager::SetNtupleRowWise(G4bool rowWise, G4bool rowMode)
{
#ifdef G4VERBOSE
// Print info even when setting makes no effect
// (as we do not get printed the default setting in the output)
G4String rowWiseMode;
if ( rowWise ) {
rowWiseMode = "row-wise with extra branch";
}
}
else if ( rowMode ) {
rowWiseMode = "row-wise";
}
else {
rowWiseMode = "column-wise";
rowWiseMode = "column-wise";
}
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()
->Message("set", "ntuple merging row mode", rowWiseMode);
#endif
Message(kVL1, "set", "ntuple merging row mode", rowWiseMode);
// Do nothing if the mode is not changed
if ( fNtupleRowWise == rowWise && fNtupleRowMode == rowMode ) return;
@@ -257,7 +208,7 @@ void G4RootNtupleFileManager::SetNtupleRowWise(G4bool rowWise, G4bool rowMode)
}
//_____________________________________________________________________________
void G4RootNtupleFileManager::SetBasketSize(unsigned int basketSize)
void G4RootNtupleFileManager::SetBasketSize(unsigned int basketSize)
{
fFileManager->SetBasketSize(basketSize);
}
@@ -275,10 +226,7 @@ std::shared_ptr<G4VNtupleManager> G4RootNtupleFileManager::CreateNtupleManager()
// If ntuple merging is activated, managers of different types are created
// on master/worker.
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("create", "ntuple manager", "");
#endif
Message(kVL4, "create", "ntuple manager");
// Check that file manager and anaysis manager are set !
@@ -286,7 +234,7 @@ std::shared_ptr<G4VNtupleManager> G4RootNtupleFileManager::CreateNtupleManager()
switch ( fNtupleMergeMode )
{
case G4NtupleMergeMode::kNone:
fNtupleManager
fNtupleManager
= make_shared<G4RootNtupleManager>(
fState, fBookingManager, 0, 0, fNtupleRowWise, fNtupleRowMode);
fNtupleManager->SetFileManager(fFileManager);
@@ -297,7 +245,7 @@ std::shared_ptr<G4VNtupleManager> G4RootNtupleFileManager::CreateNtupleManager()
G4int nofMainManagers = fNofNtupleFiles;
if ( ! nofMainManagers ) nofMainManagers = 1;
// create one manager if merging required into the histos & profiles files
fNtupleManager
fNtupleManager
= make_shared<G4RootNtupleManager>(
fState, fBookingManager, nofMainManagers, fNofNtupleFiles, fNtupleRowWise, fNtupleRowMode);
fNtupleManager->SetFileManager(fFileManager);
@@ -308,32 +256,28 @@ std::shared_ptr<G4VNtupleManager> G4RootNtupleFileManager::CreateNtupleManager()
case G4NtupleMergeMode::kSlave:
fNtupleManager = fgMasterInstance->fNtupleManager;
// The master class is used only in Get* functions
auto mainNtupleManager
= fNtupleManager->GetMainNtupleManager(GetNtupleFileNumber());
fSlaveNtupleManager
auto mainNtupleManager
= fNtupleManager->GetMainNtupleManager(GetNtupleFileNumber());
fSlaveNtupleManager
= make_shared<G4RootPNtupleManager>(
fState, fBookingManager, mainNtupleManager, fNtupleRowWise, fNtupleRowMode);
activeNtupleManager = fSlaveNtupleManager;
break;
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL3() ) {
G4String mergeMode;
switch ( fNtupleMergeMode ) {
case G4NtupleMergeMode::kNone:
mergeMode = "";
break;
case G4NtupleMergeMode::kMain:
mergeMode = "main ";
break;
case G4NtupleMergeMode::kSlave:
mergeMode = "slave ";
break;
}
fState.GetVerboseL3()->Message("create", mergeMode + "ntuple manager", "");
G4String mergeMode;
switch ( fNtupleMergeMode ) {
case G4NtupleMergeMode::kNone:
mergeMode = "";
break;
case G4NtupleMergeMode::kMain:
mergeMode = "main ";
break;
case G4NtupleMergeMode::kSlave:
mergeMode = "slave ";
break;
}
#endif
Message(kVL3, "create", mergeMode + "ntuple manager");
fIsInitialized = true;
@@ -341,37 +285,24 @@ std::shared_ptr<G4VNtupleManager> G4RootNtupleFileManager::CreateNtupleManager()
}
//_____________________________________________________________________________
#ifdef G4VERBOSE
G4bool G4RootNtupleFileManager::ActionAtOpenFile(const G4String& fileName)
#else
G4bool G4RootNtupleFileManager::ActionAtOpenFile(const G4String& /*fileName*/)
#endif
G4bool G4RootNtupleFileManager::ActionAtOpenFile([[maybe_unused]] const G4String& fileName)
{
// Check if fNtupleBookingManager is set
auto finalResult = true;
if ( fNtupleMergeMode == G4NtupleMergeMode::kNone ||
if ( fNtupleMergeMode == G4NtupleMergeMode::kNone ||
fNtupleMergeMode == G4NtupleMergeMode::kMain ) {
#ifdef G4VERBOSE
G4String objectType = "analysis file";
if ( fNtupleMergeMode == G4NtupleMergeMode::kMain ) {
objectType = "main analysis file";
}
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("open", objectType, fileName);
#endif
Message(kVL4, "open", objectType, fileName);
// Creating files is triggered from CreateNtuple
fNtupleManager->CreateNtuplesFromBooking(
fBookingManager->GetNtupleBookingVector());
#ifdef G4VERBOSE
if ( fState.GetVerboseL1() ) {
fState.GetVerboseL1()->Message("open", objectType, fileName, finalResult);
}
#endif
Message(kVL1, "open", objectType, fileName);
}
// Creating ntuples from main is triggered from the first Fill call
@@ -380,69 +311,56 @@ G4bool G4RootNtupleFileManager::ActionAtOpenFile(const G4String& /*fileName*/)
// fSlaveNtupleManager->CreateNtuplesFromMain();
// }
return finalResult;
}
return true;
}
//_____________________________________________________________________________
G4bool G4RootNtupleFileManager::ActionAtWrite()
{
if ( fNtupleMergeMode == G4NtupleMergeMode::kNone ) {
return true;
}
auto finalResult = true;
auto result = true;
G4String ntupleType;
if ( fNtupleMergeMode == G4NtupleMergeMode::kMain ) ntupleType = "main ntuples";
if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave ) ntupleType = "slave ntuples";
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("merge", ntupleType, "");
#endif
Message(kVL4, "merge", ntupleType);
if ( fNtupleMergeMode == G4NtupleMergeMode::kMain ) {
auto result = fNtupleManager->Merge();
finalResult = result && finalResult;
}
if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave ) {
auto result = fSlaveNtupleManager->Merge();
finalResult = result && finalResult;
result &= fNtupleManager->Merge();
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()->Message("merge", ntupleType, "");
#endif
return finalResult;
if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave ) {
result &= fSlaveNtupleManager->Merge();
}
Message(kVL1, "merge", ntupleType, "", result);
return result;
}
//_____________________________________________________________________________
G4bool G4RootNtupleFileManager::ActionAtCloseFile(G4bool reset)
{
auto finalResult = true;
auto result = true;
// close files
if ( fNtupleMergeMode != G4NtupleMergeMode::kSlave ) {
auto result = CloseNtupleFiles();
finalResult = finalResult && result;
result &= CloseNtupleFiles();
}
if ( ! reset ) {
// The ntuples must be always reset when closing file)
auto result = Reset();
if ( ! result ) {
G4ExceptionDescription description;
description << " " << "Resetting data failed";
G4Exception("G4RootNtupleFileManager::CloseFile()",
"Analysis_W021", JustWarning, description);
// The ntuples must be always reset when closing file)
if ( ! Reset() ) {
Warn("Resetting data failed", fkClass, "ActionAtCloseFile");
result = false;
}
finalResult = finalResult && result;
}
return finalResult;
return result;
}
//_____________________________________________________________________________
@@ -451,11 +369,11 @@ G4bool G4RootNtupleFileManager::Reset()
// Reset ntuples
auto result = true;
if ( fNtupleMergeMode == G4NtupleMergeMode::kNone ||
if ( fNtupleMergeMode == G4NtupleMergeMode::kNone ||
fNtupleMergeMode == G4NtupleMergeMode::kMain ) {
result = fNtupleManager->Reset(false);
}
result &= fNtupleManager->Reset();
}
return result;
}
+55 -63
View File
@@ -35,16 +35,14 @@
#include "tools/wroot/file"
using namespace G4Analysis;
using std::to_string;
//_____________________________________________________________________________
G4RootNtupleManager::G4RootNtupleManager(const G4AnalysisManagerState& state,
std::shared_ptr<G4NtupleBookingManager> bookingManger,
const std::shared_ptr<G4NtupleBookingManager>& bookingManger,
G4int nofMainManagers, G4int nofFiles,
G4bool rowWise, G4bool rowMode)
: G4TNtupleManager<tools::wroot::ntuple, G4RootFile>(state),
fFileManager(nullptr),
fMainNtupleManagers(),
fNtupleFile(nullptr),
fRowWise(rowWise),
fRowMode(rowMode)
{
@@ -56,15 +54,11 @@ G4RootNtupleManager::G4RootNtupleManager(const G4AnalysisManagerState& state,
}
fMainNtupleManagers.push_back(
std::make_shared<G4RootMainNtupleManager>(
this, bookingManger.get(), rowWise, fileNumber, fState));
this, bookingManger, rowWise, fileNumber, fState));
}
}
//_____________________________________________________________________________
G4RootNtupleManager::~G4RootNtupleManager()
{}
//
//
// private methods
//
@@ -75,38 +69,34 @@ void G4RootNtupleManager::CreateTNtupleFromBooking(
if ( ! fMainNtupleManagers.size() ) {
// No merging
if ( ntupleDescription->fNtuple ) {
G4String inFunction = "G4RootNtupleManager::::CreateTNtupleFromBooking";
G4ExceptionDescription description;
description << "Cannot create ntuple. Ntuple already exists." << G4endl;
G4Exception(inFunction, "Analysis_W002", JustWarning, description);
Warn("Cannot create ntuple. Ntuple already exists.",
fkClass, "CreateTNtupleFromBooking");
return;
}
}
// Create ntuple file from ntuple description
auto ntupleFile = fFileManager->CreateNtupleFile(ntupleDescription);
if ( ! ntupleFile ) {
G4String inFunction = "G4RootNtupleManager::::CreateTNtupleFromBooking";
G4ExceptionDescription description;
description << "Cannot create ntuple. Ntuple file does not exist." << G4endl;
G4Exception(inFunction, "Analysis_W002", JustWarning, description);
Warn("Cannot create ntuple. Ntuple file does not exist.",
fkClass, "CreateTNtupleFromBooking");
return;
}
auto directory = std::get<2>(*ntupleFile);
ntupleDescription->fNtuple
= new tools::wroot::ntuple(
*directory, ntupleDescription->fNtupleBooking, fRowWise);
auto basketSize = fFileManager->GetBasketSize();
ntupleDescription->fNtuple->set_basket_size(basketSize);
ntupleDescription->fIsNtupleOwner = false;
ntupleDescription->fIsNtupleOwner = false;
// ntuple object is deleted automatically when closing a file
fNtupleVector.push_back(ntupleDescription->fNtuple);
}
}
else {
// Merging activated
for ( auto manager : fMainNtupleManagers ) {
// Merging activated
for ( const auto& manager : fMainNtupleManagers ) {
manager->CreateNtuple(ntupleDescription);
}
}
@@ -120,41 +110,51 @@ void G4RootNtupleManager::FinishTNtuple(
}
//_____________________________________________________________________________
G4bool G4RootNtupleManager::Reset(G4bool deleteNtuple)
G4bool G4RootNtupleManager::Reset()
{
G4TNtupleManager<tools::wroot::ntuple, G4RootFile> ::Reset(deleteNtuple);
G4TNtupleManager<tools::wroot::ntuple, G4RootFile> ::Reset();
// this will clear ntuple vector
auto finalResult = true;
auto result = true;
for ( auto manager : fMainNtupleManagers ) {
auto result = manager->Reset(false);
finalResult = result && finalResult;
for ( const auto& manager : fMainNtupleManagers ) {
result &= manager->Reset();
}
return finalResult;
return result;
}
//_____________________________________________________________________________
void G4RootNtupleManager::Clear()
{
G4TNtupleManager<tools::wroot::ntuple, G4RootFile> ::Clear();
// this will clear ntuple vector
for ( const auto& manager : fMainNtupleManagers ) {
manager->ClearData();
}
}
//_____________________________________________________________________________
G4bool G4RootNtupleManager::Merge()
{
auto finalResult = true;
auto result = true;
for ( auto manager : fMainNtupleManagers ) {
auto result = manager->Merge();
finalResult = result && finalResult;
for ( const auto& manager : fMainNtupleManagers ) {
result &= manager->Merge();
}
return finalResult;
return result;
}
//_____________________________________________________________________________
void G4RootNtupleManager::SetFileManager(std::shared_ptr<G4RootFileManager> fileManager)
{
fFileManager = fileManager;
void G4RootNtupleManager::SetFileManager(
const std::shared_ptr<G4RootFileManager>& fileManager)
{
fFileManager = fileManager;
for ( auto mainNtupleManager : fMainNtupleManagers) {
mainNtupleManager->SetFileManager(fileManager);
for ( const auto& manager : fMainNtupleManagers ) {
manager->SetFileManager(fileManager);
}
}
@@ -176,11 +176,9 @@ std::shared_ptr<G4RootMainNtupleManager>
G4RootNtupleManager::GetMainNtupleManager(G4int index) const
{
if ( index < 0 || index >= G4int(fMainNtupleManagers.size()) ) {
G4String inFunction = "G4RootNtupleManager::::GetMainNtupleManager";
G4ExceptionDescription description;
description << " " << "main ntuple manager " << index << " does not exist.";
G4Exception(inFunction, "Analysis_W011", JustWarning, description);
return nullptr;
Warn("main ntuple manager " + to_string(index) + " does not exist.",
fkClass, "GetMainNtupleManager");
return nullptr;
}
return fMainNtupleManagers[index];
@@ -188,28 +186,22 @@ G4RootNtupleManager::GetMainNtupleManager(G4int index) const
//_____________________________________________________________________________
unsigned int G4RootNtupleManager::GetBasketSize() const
{
{
if ( ! fFileManager ) {
G4String inFunction = "G4RootNtupleManager::::GetBasketSize";
G4ExceptionDescription description;
description << " " << "File manager must be defined first.";
G4Exception(inFunction, "Analysis_W011", JustWarning, description);
return 0;
Warn("File manager must be defined first.", fkClass, "GetBasketSize");
return 0;
}
return fFileManager->GetBasketSize();
return fFileManager->GetBasketSize();
}
//_____________________________________________________________________________
unsigned int G4RootNtupleManager::GetBasketEntries() const
{
{
if ( ! fFileManager ) {
G4String inFunction = "G4RootNtupleManager::::GetBasketEntries";
G4ExceptionDescription description;
description << " " << "File manager must be defined first.";
G4Exception(inFunction, "Analysis_W011", JustWarning, description);
return 0;
Warn("File manager must be defined first.", fkClass, "GetBasketEntries");
return 0;
}
return fFileManager->GetBasketEntries();
return fFileManager->GetBasketEntries();
}
+85 -118
View File
@@ -34,6 +34,9 @@
#include "tools/wroot/file"
#include "tools/wroot/ntuple"
using namespace G4Analysis;
using std::to_string;
// mutex in a file scope
namespace {
@@ -41,13 +44,13 @@ namespace {
G4Mutex pntupleMutex = G4MUTEX_INITIALIZER;
//_____________________________________________________________________________
void NotExistException(const G4String& what, G4int id, const G4String& functionName)
void NotExistWarning(const G4String& what, G4int id,
std::string_view className,
std::string_view functionName)
{
G4String inFunction = "G4RootPNtupleManager::";
inFunction += functionName;
G4ExceptionDescription description;
description << what << " id= " << id << " does not exist.";
G4Exception(inFunction, "Analysis_W011", JustWarning, description);
Warn(what + " id= " + to_string(id) + " does not exist.",
className, functionName);
}
}
@@ -58,13 +61,10 @@ G4RootPNtupleManager::G4RootPNtupleManager(const G4AnalysisManagerState& state,
std::shared_ptr<G4RootMainNtupleManager> main,
G4bool rowWise, G4bool rowMode)
: G4BaseNtupleManager(state),
fBookingManager(bookingManger),
fMainNtupleManager(main),
fNtupleDescriptionVector(),
fNtupleVector(),
fBookingManager(std::move(bookingManger)),
fMainNtupleManager(std::move(main)),
fRowWise(rowWise),
fRowMode(rowMode),
fCreateNtuples(true)
fRowMode(rowMode)
{}
//_____________________________________________________________________________
@@ -72,7 +72,7 @@ G4RootPNtupleManager::~G4RootPNtupleManager()
{
for ( auto ntupleDescription : fNtupleDescriptionVector ) {
delete ntupleDescription;
}
}
}
//
@@ -80,53 +80,53 @@ G4RootPNtupleManager::~G4RootPNtupleManager()
//
//_____________________________________________________________________________
G4RootPNtupleDescription*
G4RootPNtupleDescription*
G4RootPNtupleManager::GetNtupleDescriptionInFunction(
G4int id, G4String functionName, G4bool warn) const
{
G4int id, std::string_view functionName, G4bool warn) const
{
auto index = id - fFirstId;
if ( index < 0 || index >= G4int(fNtupleDescriptionVector.size()) ) {
if ( warn) {
NotExistException("ntuple description", id, functionName);
NotExistWarning("ntuple description", id, fkClass, functionName);
}
return nullptr;
return nullptr;
}
return fNtupleDescriptionVector[index];
}
//_____________________________________________________________________________
tools::wroot::base_pntuple*
tools::wroot::base_pntuple*
G4RootPNtupleManager::GetNtupleInFunction(
G4int id, G4String functionName, G4bool warn) const
{
G4int id, std::string_view functionName, G4bool warn) const
{
auto ntupleDescription = GetNtupleDescriptionInFunction(id, functionName);
if ( ! ntupleDescription ) return nullptr;
if ( ! ntupleDescription->fBasePNtuple ) {
if ( warn ) {
NotExistException("ntuple", id, functionName);
NotExistWarning("ntuple", id, fkClass, functionName);
}
return nullptr;
}
}
return ntupleDescription->fBasePNtuple;
}
//_____________________________________________________________________________
tools::wroot::ntuple*
G4RootPNtupleManager::GetMainNtupleInFunction(
G4int id, G4String functionName, G4bool warn) const
{
G4int id, std::string_view functionName, G4bool warn) const
{
auto& mainNtupleVector = fMainNtupleManager->GetNtupleVector();
auto index = id - fFirstId;
if ( index < 0 || index >= G4int(mainNtupleVector.size()) ) {
if ( warn) {
NotExistException("main ntuple", id, functionName);
NotExistWarning("main ntuple", id, fkClass, functionName);
}
return nullptr;
return nullptr;
}
return mainNtupleVector[index];
}
@@ -139,20 +139,12 @@ void G4RootPNtupleManager::CreateNtupleFromMain(
G4RootPNtupleDescription* ntupleDescription,
tools::wroot::ntuple* mainNtuple)
{
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()
->Message("create from main", "pntuple", mainNtuple->name());
}
#endif
Message(kVL4, "create from main", "pntuple", mainNtuple->name());
auto file = fMainNtupleManager->GetNtupleFile(&ntupleDescription->fDescription);
if ( ! file ) {
G4String inFunction = "G4RootPNtupleManager::::CreateNtupleFromMain";
G4ExceptionDescription description;
description
<< "Cannot create pntuple. Main ntuple file does not exist." << G4endl;
G4Exception(inFunction, "Analysis_W002", JustWarning, description);
Warn("Cannot create pntuple. Main ntuple file does not exist.",
fkClass, "CreateNtupleFromMain");
return;
}
@@ -165,55 +157,50 @@ void G4RootPNtupleManager::CreateNtupleFromMain(
G4bool verbose = true;
if ( fRowWise ) {
auto mainBranch = mainNtuple->get_row_wise_branch();
tools::wroot::mt_ntuple_row_wise* mtNtuple
auto mtNtuple
= new tools::wroot::mt_ntuple_row_wise(
G4cout, rfile->byte_swap(), rfile->compression(),
mainNtuple->dir().seek_directory(),
*mainBranch, mainBranch->basket_size(),
*mainBranch, mainBranch->basket_size(),
ntupleDescription->fDescription.fNtupleBooking, verbose);
ntupleDescription->fNtuple
= static_cast<tools::wroot::imt_ntuple*>(mtNtuple);
ntupleDescription->fBasePNtuple
= static_cast<tools::wroot::base_pntuple*>(mtNtuple);
}
}
else {
std::vector<tools::uint32> basketSizes;
tools_vforcit(tools::wroot::branch*, ntupleDescription->fMainBranches, it) {
basketSizes.push_back((*it)->basket_size());
}
auto basketEntries = fMainNtupleManager->GetBasketEntries();
tools::wroot::mt_ntuple_column_wise* mtNtuple =
auto mtNtuple =
new tools::wroot::mt_ntuple_column_wise(
G4cout, rfile->byte_swap(), rfile->compression(),
mainNtuple->dir().seek_directory(),
ntupleDescription->fMainBranches, basketSizes,
ntupleDescription->fDescription.fNtupleBooking,
ntupleDescription->fDescription.fNtupleBooking,
fRowMode, basketEntries, verbose);
ntupleDescription->fNtuple
= static_cast<tools::wroot::imt_ntuple*>(mtNtuple);
ntupleDescription->fBasePNtuple
= static_cast<tools::wroot::base_pntuple*>(mtNtuple);
}
ntupleDescription->fDescription.fIsNtupleOwner = true;
ntupleDescription->fDescription.fIsNtupleOwner = true;
// // pntuple object is not deleted automatically
fNtupleVector.push_back(ntupleDescription->fNtuple);
fNtupleVector.push_back(ntupleDescription->fNtuple);
#ifdef G4VERBOSE
if ( fState.GetVerboseL3() ) {
fState.GetVerboseL3()
->Message("create from main", "pntuple", mainNtuple->name());
}
#endif
Message(kVL3, "create from main", "pntuple", mainNtuple->name());
}
//_____________________________________________________________________________
void G4RootPNtupleManager::CreateNtuplesFromMain()
{
// Create ntuple from booking (if not yet done) and main ntuple
// Create ntuple from booking (if not yet done) and main ntuple
// This function is called from the first Fill call.
// Create pntuple descriptions from ntuple booking.
@@ -221,13 +208,13 @@ void G4RootPNtupleManager::CreateNtuplesFromMain()
for ( auto g4NtupleBooking : g4NtupleBookings ) {
auto ntupleDescription = new G4RootPNtupleDescription(g4NtupleBooking);
// Save g4booking, activation in pntuple booking
fNtupleDescriptionVector.push_back(ntupleDescription);
fNtupleDescriptionVector.push_back(ntupleDescription);
}
auto& mainNtupleVector = fMainNtupleManager->GetNtupleVector();
G4int lcounter = 0;
for ( auto mainNtuple : mainNtupleVector ) {
for ( auto mainNtuple : mainNtupleVector ) {
auto& ntupleDescription = fNtupleDescriptionVector[lcounter++];
CreateNtupleFromMain(ntupleDescription, mainNtuple);
}
@@ -242,55 +229,51 @@ G4int G4RootPNtupleManager::CreateNtuple(G4NtupleBooking* /*booking*/)
// Nothing to be done here.
return G4Analysis::kInvalidId;
}
}
//_____________________________________________________________________________
G4bool G4RootPNtupleManager::FillNtupleIColumn(
G4int ntupleId, G4int columnId, G4int value)
{
return FillNtupleTColumn<int>(ntupleId, columnId, value);
}
}
//_____________________________________________________________________________
G4bool G4RootPNtupleManager::FillNtupleFColumn(
G4int ntupleId, G4int columnId, G4float value)
{
return FillNtupleTColumn<float>(ntupleId, columnId, value);
}
}
//_____________________________________________________________________________
G4bool G4RootPNtupleManager::FillNtupleDColumn(
G4int ntupleId, G4int columnId, G4double value)
{
return FillNtupleTColumn<double>(ntupleId, columnId, value);
}
}
//_____________________________________________________________________________
G4bool G4RootPNtupleManager::FillNtupleSColumn(
G4int ntupleId, G4int columnId, const G4String& value)
{
return FillNtupleTColumn<std::string>(ntupleId, columnId, value);
}
}
//_____________________________________________________________________________
G4bool G4RootPNtupleManager::AddNtupleRow(G4int ntupleId)
{
{
if (fCreateNtuples) {
CreateNtuplesFromMain();
}
if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
//G4cout << "Skipping AddNtupleRow for " << ntupleId << G4endl;
return false;
}
//G4cout << "Skipping AddNtupleRow for " << ntupleId << G4endl;
return false;
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
G4ExceptionDescription description;
description << " ntupleId " << ntupleId;
fState.GetVerboseL4()->Message("add", "pntuple row", description);
}
#endif
if ( IsVerbose(kVL4) ) {
Message(kVL4, "add", "pntuple row", " ntupleId " + to_string(ntupleId));
}
auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "AddNtupleRow");
if ( ! ntupleDescription ) return false;
@@ -300,26 +283,20 @@ G4bool G4RootPNtupleManager::AddNtupleRow(G4int ntupleId)
G4AutoLock lock(&pntupleMutex);
lock.unlock();
mutex toolsLock(lock);
auto result
auto result
= ntupleDescription->fNtuple->add_row(toolsLock, *rfile);
if ( ! result ) {
G4ExceptionDescription description;
description << " " << " ntupleId " << ntupleId
<< "adding row has failed.";
G4Exception("G4RootPNtupleManager::AddNtupleRow()",
"Analysis_W022", JustWarning, description);
Warn("NtupleId " + to_string(ntupleId) + "adding row failed.",
fkClass, "AddNtupleRow");
}
ntupleDescription->fDescription.fHasFill = true;
#ifdef G4VERBOSE
if ( fState.GetVerboseL3() ) {
G4ExceptionDescription description;
description << " ntupleId " << ntupleId;
fState.GetVerboseL3()->Message("add", "pntuple row", description);
}
#endif
if ( IsVerbose(kVL3) ) {
Message(kVL3, "add", "pntuple row", " ntupleId " + to_string(ntupleId));
}
return true;
}
@@ -335,57 +312,47 @@ G4bool G4RootPNtupleManager::Merge()
continue;
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()
->Message("merge", "pntuple", ntupleDescription->fDescription.fNtupleBooking.name());
}
#endif
if ( IsVerbose(kVL4) ) {
Message(kVL4, "merge", "pntuple", ntupleDescription->fDescription.fNtupleBooking.name());
}
auto rfile = std::get<0>(*ntupleDescription->fDescription.fFile);
G4AutoLock lock(&pntupleMutex);
lock.unlock();
mutex toolsLock(lock);
auto result
auto result
= ntupleDescription->fNtuple->end_fill(toolsLock, *rfile);
if ( ! result ) {
G4ExceptionDescription description;
description << " " << " ntuple " << ntupleDescription->fDescription.fNtupleBooking.name()
<< "end fill has failed.";
G4Exception("G4RootPNtupleManager::Merge()",
"Analysis_W031", JustWarning, description);
Warn("Ntuple " + ntupleDescription->fDescription.fNtupleBooking.name() +
"end fill has failed.", fkClass, "Merge");
}
delete ntupleDescription->fNtuple;
ntupleDescription->fNtuple = nullptr;
#ifdef G4VERBOSE
if ( fState.GetVerboseL3() ) {
fState.GetVerboseL3()
->Message("merge", "pntuple", ntupleDescription->fDescription.fNtupleBooking.name());
}
#endif
if ( IsVerbose(kVL3) ) {
Message(kVL3, "merge", "pntuple", ntupleDescription->fDescription.fNtupleBooking.name());
}
}
return true;
}
//_____________________________________________________________________________
G4bool G4RootPNtupleManager::Reset(G4bool deleteNtuple)
void G4RootPNtupleManager::Clear()
{
for ( auto ntupleDescription : fNtupleDescriptionVector ) {
if ( deleteNtuple ) {
delete ntupleDescription->fNtuple;
}
ntupleDescription->fNtuple = nullptr;
delete ntupleDescription->fNtuple;
}
fNtupleVector.clear();
return true;
}
fNtupleDescriptionVector.clear();
fNtupleVector.clear();
Message(kVL2, "clear", "pntuples");
}
//_____________________________________________________________________________
@@ -394,7 +361,7 @@ void G4RootPNtupleManager::SetActivation(
{
for ( auto ntupleDescription : fNtupleDescriptionVector ) {
ntupleDescription->fDescription.fActivation = activation;
}
}
}
//_____________________________________________________________________________
@@ -426,7 +393,7 @@ G4int G4RootPNtupleManager::GetNofNtuples() const
//_____________________________________________________________________________
void G4RootPNtupleManager::SetNtupleRowWise(G4bool rowWise, G4bool rowMode)
{
{
fRowWise = rowWise;
fRowMode = rowMode;
}
+28 -34
View File
@@ -27,28 +27,34 @@
// Author: Ivana Hrivnacova, 10/09/2014 (ivana@ipno.in2p3.fr)
#include "G4RootRFileManager.hh"
#include "G4RootHnRFileManager.hh"
#include "G4AnalysisManagerState.hh"
#include "G4AnalysisUtilities.hh"
#include "tools/rroot/file"
#include <tools/zlib>
#include "tools/zlib"
#include <iostream>
#include <cstdio>
using namespace G4Analysis;
using namespace tools;
//_____________________________________________________________________________
G4RootRFileManager::G4RootRFileManager(const G4AnalysisManagerState& state)
: G4BaseFileManager(state),
fRFiles()
: G4VRFileManager(state)
{
// Create helpers defined in the base class
fH1RFileManager = std::make_shared<G4RootHnRFileManager<histo::h1d>>(this);
fH2RFileManager = std::make_shared<G4RootHnRFileManager<histo::h2d>>(this);
fH3RFileManager = std::make_shared<G4RootHnRFileManager<histo::h3d>>(this);
fP1RFileManager = std::make_shared<G4RootHnRFileManager<histo::p1d>>(this);
fP2RFileManager = std::make_shared<G4RootHnRFileManager<histo::p2d>>(this);
}
//_____________________________________________________________________________
G4RootRFileManager::~G4RootRFileManager()
{
for (G4int i=0; i<G4int(fRFiles.size()); ++i) {
delete fRFiles[i];
}
{
for ( auto& rfile : fRFiles ) {
delete rfile.second;
}
}
//
@@ -62,56 +68,44 @@ G4bool G4RootRFileManager::OpenRFile(const G4String& fileName,
// Get full file name
G4String name = GetFullFileName(fileName, isPerThread);
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()->Message("open", "read analysis file", name);
#endif
Message(kVL4, "open", "read analysis file", name);
// create new file
tools::rroot::file* newFile = new tools::rroot::file(G4cout, name);
auto newFile = new tools::rroot::file(G4cout, name);
newFile->add_unziper('Z',tools::decompress_buffer);
if ( ! newFile->is_open() ) {
G4ExceptionDescription description;
description << " " << "Cannot open file " << name;
G4Exception("G4RootRFileManager::OpenFile()",
"Analysis_WR001", JustWarning, description);
Warn("Cannot open file " + name, fkClass, "OpenRFile");
delete newFile;
return false;
}
// add file in a map and delete the previous file if it exists
std::map<G4String, tools::rroot::file*>::iterator it
= fRFiles.find(name);
if ( it != fRFiles.end() ) {
auto it = fRFiles.find(name);
if ( it != fRFiles.end() ) {
delete it->second;
it->second = newFile;
}
else {
fRFiles[name] = newFile;
}
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()
->Message("open", "read analysis file", name);
#endif
Message(kVL1, "open", "read analysis file", name);
return true;
}
}
//_____________________________________________________________________________
tools::rroot::file* G4RootRFileManager::GetRFile(const G4String& fileName,
G4bool isPerThread) const
{
{
// Get full file name
G4String name = GetFullFileName(fileName, isPerThread);
std::map<G4String, tools::rroot::file*>::const_iterator it
= fRFiles.find(name);
auto it = fRFiles.find(name);
if ( it != fRFiles.end() )
return it->second;
else {
return nullptr;
}
}
}
+100 -19
View File
@@ -27,21 +27,112 @@
// Author: Ivana Hrivnacova, 09/04/2014 (ivana@ipno.in2p3.fr)
#include "G4RootRNtupleManager.hh"
#include "G4RootRFileManager.hh"
#include "G4AnalysisManagerState.hh"
#include "G4AnalysisUtilities.hh"
#include "tools/rroot/file"
#include "tools/rroot/rall"
#include "tools/rroot/streamers"
#include "tools/rroot/fac"
#include "tools/rroot/tree"
using namespace G4Analysis;
//_____________________________________________________________________________
G4RootRNtupleManager::G4RootRNtupleManager(const G4AnalysisManagerState& state)
: G4TRNtupleManager<tools::rroot::ntuple>(state)
{}
//_____________________________________________________________________________
G4RootRNtupleManager::~G4RootRNtupleManager()
{}
//
//
// private methods
//
//_____________________________________________________________________________
G4int G4RootRNtupleManager::ReadNtupleImpl(const G4String& ntupleName,
const G4String& fileName,
const G4String& dirName,
G4bool isUserFileName)
{
Message(kVL4, "read", "ntuple", ntupleName);
// Ntuples are saved per thread
// but do not apply the thread suffix if fileName is provided explicitly
auto isPerThread = true;
if ( isUserFileName ) isPerThread = false;
// Get or open a file
auto rfile = fFileManager->GetRFile(fileName, isPerThread);
if ( ! rfile ) {
if ( ! fFileManager->OpenRFile(fileName, isPerThread) ) return kInvalidId;
rfile = fFileManager->GetRFile(fileName, isPerThread);
}
// Get key
tools::rroot::key* key = nullptr;
tools::rroot::TDirectory* newDir = nullptr;
if ( ! dirName.empty() ) {
newDir = tools::rroot::find_dir(rfile->dir(), dirName);
if ( newDir ) {
key = newDir->find_key(ntupleName);
}
else {
G4Analysis::Warn(
"Directory " + dirName + " not found in file " + fileName + ".",
fkClass, "ReadNtupleImpl");
return kInvalidId;
}
}
else {
key = rfile->dir().find_key(ntupleName);
}
if ( ! key ) {
Warn("Key " + ntupleName + " for Ntuple not found in file " + fileName +
", directory " + dirName, fkClass, "ReadNtupleImpl");
delete newDir;
return kInvalidId;
}
unsigned int size;
char* charBuffer = key->get_object_buffer(*rfile, size);
if ( ! charBuffer ) {
Warn("Cannot get data buffer for Ntuple " + ntupleName +
" in file " + fileName, fkClass, "ReadNtupleImpl");
delete newDir;
return kInvalidId;
}
auto verbose = false;
auto buffer
= new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer,
key->key_length(), verbose);
buffer->set_map_objs(true);
auto fac = new tools::rroot::fac(G4cout);
auto tree = new tools::rroot::tree(*rfile, *fac);
if ( ! tree->stream(*buffer) ) {
Warn("TTree streaming failed for Ntuple " + ntupleName +
" in file " + fileName,
fkClass, "ReadNtupleImpl");
delete buffer;
delete tree;
delete newDir;
return kInvalidId;
}
auto rntuple = new tools::rroot::ntuple(*tree); //use the flat ntuple API.
auto rntupleDescription = new G4TRNtupleDescription<tools::rroot::ntuple>(rntuple);
auto id = SetNtuple(rntupleDescription);
Message(kVL2, "read", "ntuple", ntupleName, id > kInvalidId);
return id;
}
//_____________________________________________________________________________
G4bool G4RootRNtupleManager::GetTNtupleRow(
G4TRNtupleDescription<tools::rroot::ntuple>* ntupleDescription)
@@ -53,12 +144,7 @@ G4bool G4RootRNtupleManager::GetTNtupleRow(
if ( ! isInitialized ) {
if ( ! ntuple->initialize(G4cout, *ntupleBinding) ) {
G4ExceptionDescription description;
description
<< " "
<< "Ntuple initialization failed !!";
G4Exception("G4RootRNtuple::GetTNtupleRow()",
"Analysis_WR021", JustWarning, description);
Warn("Ntuple initialization failed !!", fkClass, "GetTNtupleRow");
return false;
}
ntupleDescription->fIsInitialized = true;
@@ -68,15 +154,10 @@ G4bool G4RootRNtupleManager::GetTNtupleRow(
auto next = ntuple->next();
if ( next ) {
if ( ! ntuple->get_row() ) {
G4ExceptionDescription description;
description
<< " "
<< "Ntuple get_row() failed !!";
G4Exception("G4RootRNtuple::GetTNtupleRow()",
"Analysis_WR021", JustWarning, description);
Warn("Ntuple get_row() failed !!", fkClass, "GetTNtupleRow");
return false;
}
}
}
return next;
}
}