Import Geant4 10.3.0 source tree
This commit is contained in:
@@ -23,15 +23,18 @@
|
||||
// * acceptance of all terms of the Geant4 Software license. *
|
||||
// ********************************************************************
|
||||
//
|
||||
// $Id: G4RootAnalysisManager.cc 93815 2015-11-02 11:32:20Z gcosmo $
|
||||
// $Id: G4RootAnalysisManager.cc 100648 2016-10-31 10:06:45Z gcosmo $
|
||||
|
||||
// Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
|
||||
|
||||
#include "G4RootAnalysisManager.hh"
|
||||
#include "G4RootFileManager.hh"
|
||||
#include "G4RootNtupleManager.hh"
|
||||
#include "G4RootMainNtupleManager.hh"
|
||||
#include "G4RootPNtupleManager.hh"
|
||||
#include "G4AnalysisVerbose.hh"
|
||||
#include "G4AnalysisManagerState.hh"
|
||||
|
||||
#include "G4Threading.hh"
|
||||
#include "G4AutoLock.hh"
|
||||
|
||||
@@ -76,7 +79,10 @@ G4bool G4RootAnalysisManager::IsInstance()
|
||||
//_____________________________________________________________________________
|
||||
G4RootAnalysisManager::G4RootAnalysisManager(G4bool isMaster)
|
||||
: G4ToolsAnalysisManager("Root", isMaster),
|
||||
fNofNtupleFiles(0),
|
||||
fNtupleMergeMode(G4NtupleMergeMode::kNone),
|
||||
fNtupleManager(nullptr),
|
||||
fSlaveNtupleManager(nullptr),
|
||||
fFileManager(nullptr)
|
||||
{
|
||||
if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
|
||||
@@ -91,16 +97,21 @@ G4RootAnalysisManager::G4RootAnalysisManager(G4bool isMaster)
|
||||
if ( isMaster ) fgMasterInstance = this;
|
||||
fgInstance = this;
|
||||
|
||||
// Create managers
|
||||
fNtupleManager = new G4RootNtupleManager(fState);
|
||||
// File manager
|
||||
fFileManager = std::make_shared<G4RootFileManager>(fState);
|
||||
// The managers will be deleted by the base class
|
||||
|
||||
// Set managers to base class which takes then their ownership
|
||||
SetNtupleManager(fNtupleManager);
|
||||
SetFileManager(fFileManager);
|
||||
}
|
||||
fFileManager->SetBasketSize(fgkDefaultBasketSize);
|
||||
|
||||
// Do not merge ntuples by default
|
||||
// Merging may require user code migration as analysis manager
|
||||
// must be created both on master and workers.
|
||||
auto mergeNtuples = false;
|
||||
SetNtupleMergingMode(mergeNtuples, fNofNtupleFiles);
|
||||
|
||||
// Create ntuple managers
|
||||
CreateNtupleManagers();
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4RootAnalysisManager::~G4RootAnalysisManager()
|
||||
{
|
||||
@@ -112,6 +123,189 @@ G4RootAnalysisManager::~G4RootAnalysisManager()
|
||||
// private methods
|
||||
//
|
||||
|
||||
//_____________________________________________________________________________
|
||||
void G4RootAnalysisManager::SetNtupleMergingMode(G4bool mergeNtuples,
|
||||
G4int nofNtupleFiles)
|
||||
|
||||
{
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL4() )
|
||||
fState.GetVerboseL4()
|
||||
->Message("set", "ntuple merging mode", "");
|
||||
#endif
|
||||
|
||||
auto canMerge = true;
|
||||
|
||||
// Illegal situations
|
||||
if ( mergeNtuples && ( ! G4Threading::IsMultithreadedApplication() ) ) {
|
||||
if ( nofNtupleFiles > 0 ) {
|
||||
G4ExceptionDescription description;
|
||||
description
|
||||
<< " " << "Merging ntuples is not applicable in sequential application."
|
||||
<< G4endl
|
||||
<< " " << "Setting was ignored.";
|
||||
G4Exception("G4RootAnalysisManager::SetNtupleMergingMode()",
|
||||
"Analysis_W013", JustWarning, description);
|
||||
}
|
||||
canMerge = false;
|
||||
}
|
||||
|
||||
// Illegal situations
|
||||
if ( mergeNtuples && G4Threading::IsMultithreadedApplication() &&
|
||||
( ! fgMasterInstance ) ) {
|
||||
G4ExceptionDescription description;
|
||||
description
|
||||
<< " " << "Merging ntuples requires G4AnalysisManager instance on master."
|
||||
<< G4endl
|
||||
<< " " << "Setting was ignored.";
|
||||
G4Exception("G4RootAnalysisManager::SetNtupleMergingMode()",
|
||||
"Analysis_W013", JustWarning, description);
|
||||
canMerge = false;
|
||||
}
|
||||
|
||||
G4String mergingMode;
|
||||
if ( ( ! mergeNtuples ) || ( ! canMerge ) ) {
|
||||
fNtupleMergeMode = G4NtupleMergeMode::kNone;
|
||||
mergingMode = "G4NtupleMergeMode::kNone";
|
||||
}
|
||||
else {
|
||||
// Set the number of reduced ntuple files
|
||||
// G4int nofThreads = G4Threading::GetNumberOfThreads();
|
||||
fNofNtupleFiles = nofNtupleFiles;
|
||||
|
||||
// Check the number of reduced ntuple files
|
||||
// if ( fNofNtupleFiles < 0 || fNofNtupleFiles > nofThreads ) {
|
||||
if ( fNofNtupleFiles < 0 ) {
|
||||
G4ExceptionDescription description;
|
||||
description
|
||||
<< " " << "Number of reduced files must be [0, nofThreads]."
|
||||
<< G4endl
|
||||
<< " " << "Cannot set " << nofNtupleFiles
|
||||
// << " files when nofThreads is " << nofThreads << G4endl
|
||||
<< " files" << G4endl
|
||||
<< " " << "Ntuples will be merged in a single file.";
|
||||
G4Exception("G4RootAnalysisManager::SetNtupleMergingMode()",
|
||||
"Analysis_W013", JustWarning, description);
|
||||
fNofNtupleFiles = 0;
|
||||
}
|
||||
|
||||
// if ( fNofNtupleFiles == nofThreads ) {
|
||||
// // add warning that no merging will be applied
|
||||
// fNtupleMergeMode = G4NtupleMergeMode::kNone;
|
||||
// fNofNtupleFiles = 0;
|
||||
// mergingMode = "G4NtupleMergeMode::kNone";
|
||||
// }
|
||||
// else {
|
||||
// G4bool isMaster = ! G4Threading::IsWorkerThread();
|
||||
// if ( isMaster ) {
|
||||
// fNtupleMergeMode = G4NtupleMergeMode::kMain;
|
||||
// mergingMode = "G4NtupleMergeMode::kMain";
|
||||
// } else {
|
||||
// fNtupleMergeMode = G4NtupleMergeMode::kSlave;
|
||||
// mergingMode = "G4NtupleMergeMode::kSlave";
|
||||
// }
|
||||
// }
|
||||
|
||||
// Forced merging mode
|
||||
G4bool isMaster = ! G4Threading::IsWorkerThread();
|
||||
if ( isMaster ) {
|
||||
fNtupleMergeMode = G4NtupleMergeMode::kMain;
|
||||
mergingMode = "G4NtupleMergeMode::kMain";
|
||||
} else {
|
||||
fNtupleMergeMode = G4NtupleMergeMode::kSlave;
|
||||
mergingMode = "G4NtupleMergeMode::kSlave";
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL2() )
|
||||
fState.GetVerboseL2()
|
||||
->Message("set", "ntuple merging mode", mergingMode);
|
||||
#endif
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
void G4RootAnalysisManager::ClearNtupleManagers()
|
||||
{
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL4() )
|
||||
fState.GetVerboseL4()->Message("clear", "ntuple managers", "");
|
||||
#endif
|
||||
|
||||
if ( fNtupleMergeMode != G4NtupleMergeMode::kSlave ) {
|
||||
// Do not reset master ntuple manager
|
||||
delete fNtupleManager;
|
||||
fNtupleManager = nullptr;
|
||||
// SetNtupleManager(fNtupleManager);
|
||||
}
|
||||
|
||||
delete fSlaveNtupleManager;
|
||||
fSlaveNtupleManager = nullptr;
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL3() )
|
||||
fState.GetVerboseL3()->Message("clear", "ntuple managers", "");
|
||||
#endif
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
void G4RootAnalysisManager::CreateNtupleManagers()
|
||||
{
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL4() )
|
||||
fState.GetVerboseL4()->Message("create", "ntuple managers", "");
|
||||
#endif
|
||||
|
||||
switch ( fNtupleMergeMode )
|
||||
{
|
||||
case G4NtupleMergeMode::kNone:
|
||||
fNtupleManager = new G4RootNtupleManager(fState);
|
||||
fNtupleManager->SetFileManager(fFileManager);
|
||||
SetNtupleManager(fNtupleManager);
|
||||
break;
|
||||
|
||||
case G4NtupleMergeMode::kMain: {
|
||||
G4int nofMainManagers = fNofNtupleFiles;
|
||||
if ( ! nofMainManagers ) nofMainManagers = 1;
|
||||
// create one manager if merging required into the histos & profiles files
|
||||
fNtupleManager = new G4RootNtupleManager(fState, nofMainManagers);
|
||||
fNtupleManager->SetFileManager(fFileManager);
|
||||
SetNtupleManager(fNtupleManager);
|
||||
break;
|
||||
}
|
||||
|
||||
case G4NtupleMergeMode::kSlave:
|
||||
fNtupleManager = fgMasterInstance->fNtupleManager;
|
||||
// The master class is used only in Get* functions
|
||||
auto mainNtupleManager
|
||||
= fNtupleManager->GetMainNtupleManager(GetNtupleFileNumber());
|
||||
fSlaveNtupleManager = new G4RootPNtupleManager(mainNtupleManager, fState);
|
||||
SetNtupleManager(fSlaveNtupleManager);
|
||||
break;
|
||||
}
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL3() )
|
||||
fState.GetVerboseL3()->Message("create", "ntuple managers", "");
|
||||
#endif
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4int G4RootAnalysisManager::GetNtupleFileNumber()
|
||||
{
|
||||
if ( ! fNofNtupleFiles ) return 0;
|
||||
|
||||
G4int nofMainManagers = fNofNtupleFiles;
|
||||
if ( ! nofMainManagers ) nofMainManagers = 1;
|
||||
|
||||
// Debug - check G4Threading::GetNumberOfRunningWorkerThreads()
|
||||
G4cout << "In GetNtupleFileNumber: "
|
||||
<< G4Threading::GetNumberOfRunningWorkerThreads() << G4endl;
|
||||
|
||||
auto fileNumber = G4Threading::G4GetThreadId() % nofMainManagers;
|
||||
return fileNumber;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootAnalysisManager::WriteH1()
|
||||
{
|
||||
@@ -237,6 +431,40 @@ G4bool G4RootAnalysisManager::WriteP2()
|
||||
return result;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootAnalysisManager::WriteNtuple()
|
||||
{
|
||||
if ( fNtupleMergeMode == G4NtupleMergeMode::kNone ) return true;
|
||||
|
||||
auto finalResult = 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
|
||||
|
||||
if ( fNtupleMergeMode == G4NtupleMergeMode::kMain ) {
|
||||
auto result = fNtupleManager->Merge();
|
||||
finalResult = result && finalResult;
|
||||
}
|
||||
|
||||
if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave ) {
|
||||
auto result = fSlaveNtupleManager->Merge();
|
||||
finalResult = result && finalResult;
|
||||
}
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL1() )
|
||||
fState.GetVerboseL1()->Message("merge", ntupleType, "");
|
||||
#endif
|
||||
|
||||
return finalResult;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootAnalysisManager::Reset()
|
||||
{
|
||||
@@ -247,12 +475,17 @@ G4bool G4RootAnalysisManager::Reset()
|
||||
auto result = G4ToolsAnalysisManager::Reset();
|
||||
finalResult = finalResult && result;
|
||||
|
||||
result = fNtupleManager->Reset(false);
|
||||
if ( fNtupleMergeMode == G4NtupleMergeMode::kNone ||
|
||||
fNtupleMergeMode == G4NtupleMergeMode::kMain ) {
|
||||
result = fNtupleManager->Reset(false);
|
||||
finalResult = result && finalResult;
|
||||
}
|
||||
|
||||
finalResult = finalResult && result;
|
||||
|
||||
return finalResult;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
// protected methods
|
||||
//
|
||||
@@ -264,23 +497,52 @@ G4bool G4RootAnalysisManager::OpenFileImpl(const G4String& fileName)
|
||||
auto result = fFileManager->SetFileName(fileName);
|
||||
finalResult = finalResult && result;
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
G4String name = fFileManager->GetFullFileName();
|
||||
if ( fState.GetVerboseL4() )
|
||||
fState.GetVerboseL4()->Message("open", "analysis file", name);
|
||||
#endif
|
||||
|
||||
result = fFileManager->OpenFile(fileName);
|
||||
finalResult = finalResult && result;
|
||||
|
||||
fNtupleManager->SetNtupleDirectory(fFileManager->GetNtupleDirectory());
|
||||
fNtupleManager->CreateNtuplesFromBooking();
|
||||
if ( fNtupleMergeMode == G4NtupleMergeMode::kNone ) {
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL1() )
|
||||
fState.GetVerboseL1()->Message("open", "analysis file", name);
|
||||
G4String name = fFileManager->GetFullFileName();
|
||||
if ( fState.GetVerboseL4() )
|
||||
fState.GetVerboseL4()->Message("open", "analysis file", name);
|
||||
#endif
|
||||
|
||||
result = fFileManager->OpenFile(fileName);
|
||||
finalResult = finalResult && result;
|
||||
|
||||
fNtupleManager->SetNtupleDirectory(fFileManager->GetNtupleDirectory());
|
||||
fNtupleManager->CreateNtuplesFromBooking();
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL1() )
|
||||
fState.GetVerboseL1()->Message("open", "analysis file", name);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
if ( fNtupleMergeMode == G4NtupleMergeMode::kMain ) {
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
G4String name = fFileManager->GetFullFileName();
|
||||
if ( fState.GetVerboseL4() )
|
||||
fState.GetVerboseL4()->Message("open", "main analysis file", name);
|
||||
#endif
|
||||
|
||||
fFileManager->SetNofNtupleFiles(fNofNtupleFiles);
|
||||
result = fFileManager->OpenFile(fileName);
|
||||
finalResult = finalResult && result;
|
||||
|
||||
fNtupleManager->CreateNtuplesFromBooking();
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL1() )
|
||||
fState.GetVerboseL1()->Message("open", "main analysis file", name);
|
||||
#endif
|
||||
}
|
||||
|
||||
if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave ) {
|
||||
// No file is open by Slave manager
|
||||
fSlaveNtupleManager->CreateNtuplesFromMain();
|
||||
}
|
||||
|
||||
return finalResult;
|
||||
}
|
||||
|
||||
@@ -323,10 +585,16 @@ G4bool G4RootAnalysisManager::WriteImpl()
|
||||
result = WriteP2();
|
||||
finalResult = finalResult && result;
|
||||
|
||||
// File
|
||||
result = fFileManager->WriteFile();
|
||||
// Ntuples
|
||||
result = WriteNtuple();
|
||||
finalResult = finalResult && result;
|
||||
|
||||
// File
|
||||
if ( fNtupleMergeMode != G4NtupleMergeMode::kSlave ) {
|
||||
result = fFileManager->WriteFile();
|
||||
finalResult = finalResult && result;
|
||||
}
|
||||
|
||||
// Write ASCII if activated
|
||||
if ( IsAscii() ) {
|
||||
result = WriteAscii(fFileManager->GetFileName());
|
||||
@@ -341,12 +609,6 @@ G4bool G4RootAnalysisManager::CloseFileImpl()
|
||||
{
|
||||
auto finalResult = true;
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL4() )
|
||||
fState.GetVerboseL4()
|
||||
->Message("close", "file", fFileManager->GetFullFileName());
|
||||
#endif
|
||||
|
||||
// reset data
|
||||
auto result = Reset();
|
||||
if ( ! result ) {
|
||||
@@ -357,40 +619,33 @@ G4bool G4RootAnalysisManager::CloseFileImpl()
|
||||
}
|
||||
finalResult = finalResult && result;
|
||||
|
||||
// close file
|
||||
fFileManager->CloseFile();
|
||||
|
||||
// No files clean-up in sequential mode
|
||||
if ( ! G4Threading::IsMultithreadedApplication() ) return finalResult;
|
||||
|
||||
// Delete files if empty in MT mode
|
||||
if ( ( fState.GetIsMaster() &&
|
||||
fH1Manager->IsEmpty() && fH2Manager->IsEmpty() && fH3Manager->IsEmpty() &&
|
||||
fP1Manager->IsEmpty() && fP2Manager->IsEmpty() && fNtupleManager->IsEmpty() ) ||
|
||||
( ( ! fState.GetIsMaster() ) && fNtupleManager->IsEmpty() ) ) {
|
||||
result = ! std::remove(fFileManager->GetFullFileName());
|
||||
// std::remove returns 0 when success
|
||||
if ( ! result ) {
|
||||
G4ExceptionDescription description;
|
||||
description << " " << "Removing file "
|
||||
<< fFileManager->GetFullFileName() << " failed";
|
||||
G4Exception("G4XmlAnalysisManager::CloseFile()",
|
||||
"Analysis_W021", JustWarning, description);
|
||||
}
|
||||
finalResult = finalResult && result;
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL1() )
|
||||
fState.GetVerboseL1()
|
||||
->Message("delete", "empty file", fFileManager->GetFullFileName());
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL1() )
|
||||
fState.GetVerboseL1()
|
||||
->Message("close", "file", fFileManager->GetFullFileName());
|
||||
#endif
|
||||
if ( fNtupleMergeMode != G4NtupleMergeMode::kSlave ) {
|
||||
// close file
|
||||
fFileManager->CloseFile();
|
||||
}
|
||||
|
||||
return finalResult;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// public methods
|
||||
//
|
||||
|
||||
//_____________________________________________________________________________
|
||||
void G4RootAnalysisManager::SetNtupleMerging(G4bool mergeNtuples,
|
||||
G4int nofNtupleFiles,
|
||||
unsigned int basketSize)
|
||||
|
||||
{
|
||||
// Keep basketSize in file manager
|
||||
fFileManager->SetBasketSize(basketSize);
|
||||
|
||||
// Set ntuple merging mode
|
||||
SetNtupleMergingMode(mergeNtuples, nofNtupleFiles);
|
||||
|
||||
// Clear existing managers
|
||||
ClearNtupleManagers();
|
||||
|
||||
// Re-create managers
|
||||
CreateNtupleManagers();
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ tools::rroot::buffer* G4RootAnalysisReader::GetBuffer(
|
||||
//char* charBuffer
|
||||
// = ( ! key ) ? 0 : key->get_object_buffer(size);
|
||||
char* charBuffer = 0;
|
||||
if ( key ) charBuffer = key->get_object_buffer(size);
|
||||
if ( key ) charBuffer = key->get_object_buffer(*rfile, size);
|
||||
|
||||
if ( ! charBuffer ) {
|
||||
G4ExceptionDescription description;
|
||||
@@ -387,7 +387,7 @@ G4int G4RootAnalysisReader::ReadNtupleImpl(const G4String& ntupleName,
|
||||
}
|
||||
|
||||
unsigned int size;
|
||||
char* charBuffer = key->get_object_buffer(size);
|
||||
char* charBuffer = key->get_object_buffer(*rfile, size);
|
||||
if ( ! charBuffer ) {
|
||||
G4ExceptionDescription description;
|
||||
description
|
||||
@@ -402,7 +402,7 @@ G4int G4RootAnalysisReader::ReadNtupleImpl(const G4String& ntupleName,
|
||||
auto buffer
|
||||
= new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer,
|
||||
key->key_length(), verbose);
|
||||
auto fac = new tools::rroot::fac(*rfile);
|
||||
auto fac = new tools::rroot::fac(G4cout);
|
||||
|
||||
auto tree = new tools::rroot::tree(*rfile, *fac);
|
||||
if ( ! tree->stream(*buffer) ) {
|
||||
|
||||
@@ -32,8 +32,7 @@
|
||||
#include "G4AnalysisUtilities.hh"
|
||||
|
||||
#include "tools/wroot/file"
|
||||
#include "tools/rroot/file"
|
||||
#include <tools/zlib>
|
||||
#include "tools/zlib"
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
@@ -45,13 +44,126 @@ G4RootFileManager::G4RootFileManager(const G4AnalysisManagerState& state)
|
||||
: G4VFileManager(state),
|
||||
fFile(nullptr),
|
||||
fHistoDirectory(nullptr),
|
||||
fNtupleDirectory(nullptr)
|
||||
fNtupleDirectory(nullptr),
|
||||
fNofNtupleFiles(0),
|
||||
fNtupleFiles(),
|
||||
fMainNtupleDirectories(),
|
||||
fBasketSize(0)
|
||||
{}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4RootFileManager::~G4RootFileManager()
|
||||
{}
|
||||
|
||||
//
|
||||
// private methods
|
||||
//
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootFileManager::OpenNtupleFiles()
|
||||
{
|
||||
auto finalResult = true;
|
||||
|
||||
for ( auto i = 0; i < fNofNtupleFiles; i++ ) {
|
||||
|
||||
auto name = GetNtupleFileName(i);
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL4() )
|
||||
fState.GetVerboseL4()
|
||||
->Message("create", "main ntuple file", name);
|
||||
#endif
|
||||
|
||||
// create new file
|
||||
auto rfile = std::make_shared<tools::wroot::file>(G4cout, name);
|
||||
rfile->add_ziper('Z', tools::compress_buffer);
|
||||
rfile->set_compression(fState.GetCompressionLevel());
|
||||
|
||||
if ( ! rfile->is_open() ) {
|
||||
G4ExceptionDescription description;
|
||||
description << " " << "Cannot open file " << name;
|
||||
G4Exception("G4RootAnalysisManager::OpenFile()",
|
||||
"Analysis_W001", JustWarning, description);
|
||||
finalResult = false;
|
||||
}
|
||||
|
||||
// Do not create directory if extra ntuple files
|
||||
// auto result = CreateNtupleDirectory(rfile);
|
||||
// finalResult = finalResult && result;
|
||||
tools::wroot::directory* directory = &rfile->dir();
|
||||
if ( fNtupleDirectoryName != "" ) {
|
||||
directory = rfile->dir().mkdir(fNtupleDirectoryName);
|
||||
if ( ! directory ) {
|
||||
G4ExceptionDescription description;
|
||||
description << " "
|
||||
<< "cannot create directory " << fNtupleDirectoryName;
|
||||
G4Exception("G4RootFileManager::OpenNtupleFiles()",
|
||||
"Analysis_W001", JustWarning, description);
|
||||
directory = &fFile->dir();
|
||||
}
|
||||
}
|
||||
|
||||
fNtupleFiles.push_back(rfile);
|
||||
fMainNtupleDirectories.push_back(directory);
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL1() )
|
||||
fState.GetVerboseL1()
|
||||
->Message("create", "main ntuple file", name);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
return finalResult;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootFileManager::WriteFile(std::shared_ptr<tools::wroot::file> rfile,
|
||||
#ifdef G4VERBOSE
|
||||
const G4String& fileName)
|
||||
#else
|
||||
const G4String& /*fileName*/)
|
||||
#endif
|
||||
{
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL4() )
|
||||
fState.GetVerboseL4()->Message("write", "file", fileName);
|
||||
#endif
|
||||
|
||||
unsigned int n;
|
||||
auto result = rfile->write(n);
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL1() )
|
||||
fState.GetVerboseL1()->Message("write", "file", fileName, result);
|
||||
#endif
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootFileManager::CloseFile(std::shared_ptr<tools::wroot::file> rfile,
|
||||
#ifdef G4VERBOSE
|
||||
const G4String& fileName)
|
||||
#else
|
||||
const G4String& /*fileName*/)
|
||||
#endif
|
||||
{
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL4() )
|
||||
fState.GetVerboseL4()->Message("close", "file", fileName);
|
||||
#endif
|
||||
|
||||
rfile->close();
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL1() )
|
||||
fState.GetVerboseL1()->Message("close", "file", fileName, true);
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
// public methods
|
||||
//
|
||||
@@ -66,7 +178,7 @@ G4bool G4RootFileManager::OpenFile(const G4String& fileName)
|
||||
//if ( fFile ) delete fFile;
|
||||
|
||||
// create new file
|
||||
fFile = G4Analysis::make_unique<tools::wroot::file>(G4cout, name);
|
||||
fFile = std::make_shared<tools::wroot::file>(G4cout, name);
|
||||
fFile->add_ziper('Z',tools::compress_buffer);
|
||||
fFile->set_compression(fState.GetCompressionLevel());
|
||||
|
||||
@@ -82,6 +194,9 @@ G4bool G4RootFileManager::OpenFile(const G4String& fileName)
|
||||
if ( ! CreateHistoDirectory() ) return false;
|
||||
if ( ! CreateNtupleDirectory() ) return false;
|
||||
|
||||
// Open ntuple files
|
||||
OpenNtupleFiles();
|
||||
|
||||
fLockFileName = true;
|
||||
fLockHistoDirectoryName = true;
|
||||
fLockNtupleDirectoryName = true;
|
||||
@@ -90,35 +205,41 @@ G4bool G4RootFileManager::OpenFile(const G4String& fileName)
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootFileManager:: WriteFile()
|
||||
{
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL4() )
|
||||
fState.GetVerboseL4()->Message("write", "file", GetFullFileName());
|
||||
#endif
|
||||
auto finalResult = true;
|
||||
|
||||
unsigned int n;
|
||||
auto result = fFile->write(n);
|
||||
auto result = WriteFile(fFile, GetFullFileName());
|
||||
finalResult = finalResult && result;
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL1() )
|
||||
fState.GetVerboseL1()->Message("write", "file", GetFullFileName(), result);
|
||||
#endif
|
||||
|
||||
return result;
|
||||
auto counter = 0;
|
||||
for ( auto ntupleFile : fNtupleFiles ) {
|
||||
result = WriteFile(ntupleFile, GetNtupleFileName(counter++));
|
||||
finalResult = finalResult && result;
|
||||
}
|
||||
return finalResult;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootFileManager::CloseFile()
|
||||
{
|
||||
// close file
|
||||
fFile->close();
|
||||
auto finalResult = true;
|
||||
|
||||
auto result = CloseFile(fFile, GetFullFileName());
|
||||
finalResult = finalResult && result;
|
||||
|
||||
auto counter = 0;
|
||||
for ( auto ntupleFile : fNtupleFiles ) {
|
||||
result = CloseFile(ntupleFile, GetNtupleFileName(counter++));
|
||||
finalResult = finalResult && result;
|
||||
}
|
||||
|
||||
fLockFileName = false;
|
||||
fIsOpenFile = false;
|
||||
|
||||
return true;
|
||||
return finalResult;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
@@ -188,3 +309,38 @@ G4bool G4RootFileManager::CreateNtupleDirectory()
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
std::shared_ptr<tools::wroot::file>
|
||||
G4RootFileManager::GetNtupleFile(G4int index) const
|
||||
{
|
||||
if ( index==0 && ( ! fNtupleFiles.size() ) ) return fFile;
|
||||
|
||||
if ( index < 0 || index >= G4int(fNtupleFiles.size()) ) {
|
||||
G4String inFunction = "G4RootFileManager::GetNtupleFile()";
|
||||
G4ExceptionDescription description;
|
||||
description << " " << "ntuple file " << index << " does not exist.";
|
||||
G4Exception(inFunction, "Analysis_W011", JustWarning, description);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return fNtupleFiles[index];
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
tools::wroot::directory*
|
||||
G4RootFileManager::GetMainNtupleDirectory(G4int index) const
|
||||
{
|
||||
if ( index==0 && ( ! fMainNtupleDirectories.size() ) ) return fNtupleDirectory;
|
||||
|
||||
if ( index < 0 || index >= G4int(fMainNtupleDirectories.size()) ) {
|
||||
G4String inFunction = "G4RootFileManager::GetMainNtupleDirectory()";
|
||||
G4ExceptionDescription description;
|
||||
description << " " << "main ntuple directory " << index << " does not exist.";
|
||||
G4Exception(inFunction, "Analysis_W011", JustWarning, description);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return fMainNtupleDirectories[index];
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
//
|
||||
// ********************************************************************
|
||||
// * License and Disclaimer *
|
||||
// * *
|
||||
// * The Geant4 software is copyright of the Copyright Holders of *
|
||||
// * the Geant4 Collaboration. It is provided under the terms and *
|
||||
// * conditions of the Geant4 Software License, included in the file *
|
||||
// * LICENSE and available at http://cern.ch/geant4/license . These *
|
||||
// * include a list of copyright holders. *
|
||||
// * *
|
||||
// * Neither the authors of this software system, nor their employing *
|
||||
// * institutes,nor the agencies providing financial support for this *
|
||||
// * work make any representation or warranty, express or implied, *
|
||||
// * regarding this software system or assume any liability for its *
|
||||
// * use. Please see the license in the file LICENSE and URL above *
|
||||
// * for the full disclaimer and the limitation of liability. *
|
||||
// * *
|
||||
// * This code implementation is the result of the scientific and *
|
||||
// * technical work of the GEANT4 collaboration. *
|
||||
// * By using, copying, modifying or distributing the software (or *
|
||||
// * any work based on the software) you agree to acknowledge its *
|
||||
// * use in resulting scientific publications, and indicate your *
|
||||
// * acceptance of all terms of the Geant4 Software license. *
|
||||
// ********************************************************************
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Author: Ivana Hrivnacova, 04/10/2016 (ivana@ipno.in2p3.fr)
|
||||
|
||||
#include "G4RootMainNtupleManager.hh"
|
||||
#include "G4RootNtupleManager.hh"
|
||||
#include "G4AnalysisUtilities.hh"
|
||||
|
||||
#include "tools/wroot/file"
|
||||
#include "tools/wroot/ntuple"
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4RootMainNtupleManager::G4RootMainNtupleManager(G4RootNtupleManager* ntupleBuilder,
|
||||
const G4AnalysisManagerState& state)
|
||||
: G4BaseAnalysisManager(state),
|
||||
fNtupleBuilder(ntupleBuilder),
|
||||
fNtupleDirectory(nullptr),
|
||||
fNtupleVector()
|
||||
{}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4RootMainNtupleManager::~G4RootMainNtupleManager()
|
||||
{
|
||||
// ntuple objects are deleted automatically when closing a file
|
||||
}
|
||||
|
||||
//
|
||||
// private functions
|
||||
//
|
||||
|
||||
//
|
||||
// protected functions
|
||||
//
|
||||
|
||||
//_____________________________________________________________________________
|
||||
void G4RootMainNtupleManager::CreateNtuple(const tools::ntuple_booking& ntupleBooking,
|
||||
G4bool warn)
|
||||
{
|
||||
// Create ntuple from booking if file was open
|
||||
|
||||
// Check that file is set
|
||||
if ( ! fNtupleDirectory ) {
|
||||
if ( warn ) {
|
||||
G4ExceptionDescription description;
|
||||
description
|
||||
<< " " << "Ntuple file must be defined first."
|
||||
<< G4endl
|
||||
<< " " << "Cannot create main ntuples from builder.";
|
||||
G4Exception("G4RootAnalysisManager::CreateNtuplesFromBooking",
|
||||
"Analysis_W002", JustWarning, description);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL4() )
|
||||
fState.GetVerboseL4()
|
||||
->Message("create", "main ntuple", ntupleBooking.name());
|
||||
#endif
|
||||
|
||||
// Create ntuple
|
||||
auto ntuple = new tools::wroot::ntuple(*fNtupleDirectory, ntupleBooking);
|
||||
// ntuple object is deleted automatically when closing a file
|
||||
auto basketSize = fNtupleBuilder->GetBasketSize();
|
||||
ntuple->set_basket_size(basketSize);
|
||||
|
||||
fNtupleVector.push_back(ntuple);
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL3() )
|
||||
fState.GetVerboseL3()
|
||||
->Message("create", "main ntuple", ntupleBooking.name());
|
||||
#endif
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
void G4RootMainNtupleManager::CreateNtuplesFromBooking()
|
||||
{
|
||||
// Create ntuple from booking in master
|
||||
|
||||
// Check that file is set
|
||||
if ( ! fNtupleDirectory ) {
|
||||
G4ExceptionDescription description;
|
||||
description
|
||||
<< " " << "Ntuple file must be defined first."
|
||||
<< G4endl
|
||||
<< " " << "Cannot create main ntuples from builder.";
|
||||
G4Exception("G4RootAnalysisManager::CreateNtuplesFromBooking",
|
||||
"Analysis_W002", JustWarning, description);
|
||||
return;
|
||||
}
|
||||
|
||||
auto& ntupleDescriptionVector
|
||||
= fNtupleBuilder->GetNtupleDescriptionVector();
|
||||
|
||||
for ( auto ntupleDescription : ntupleDescriptionVector ) {
|
||||
CreateNtuple(ntupleDescription->fNtupleBooking);
|
||||
}
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootMainNtupleManager::Merge()
|
||||
{
|
||||
for ( auto ntuple : fNtupleVector ) {
|
||||
ntuple->merge_number_of_entries();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootMainNtupleManager::Reset(G4bool deleteNtuple)
|
||||
{
|
||||
for ( auto ntuple : fNtupleVector ) {
|
||||
if ( deleteNtuple ) {
|
||||
delete ntuple;
|
||||
}
|
||||
}
|
||||
|
||||
fNtupleVector.clear();
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -28,6 +28,8 @@
|
||||
// Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
|
||||
|
||||
#include "G4RootNtupleManager.hh"
|
||||
#include "G4RootMainNtupleManager.hh"
|
||||
#include "G4RootFileManager.hh"
|
||||
#include "G4AnalysisManagerState.hh"
|
||||
#include "G4AnalysisUtilities.hh"
|
||||
|
||||
@@ -36,10 +38,19 @@
|
||||
using namespace G4Analysis;
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4RootNtupleManager::G4RootNtupleManager(const G4AnalysisManagerState& state)
|
||||
G4RootNtupleManager::G4RootNtupleManager(const G4AnalysisManagerState& state,
|
||||
G4int nofMainManagers)
|
||||
: G4TNtupleManager<tools::wroot::ntuple>(state),
|
||||
fNtupleDirectory(nullptr)
|
||||
{}
|
||||
fCreateMode(G4NtupleCreateMode::kUndefined),
|
||||
fFileManager(nullptr),
|
||||
fNtupleDirectory(nullptr),
|
||||
fMainNtupleManagers()
|
||||
{
|
||||
for ( G4int i=0; i<nofMainManagers; ++i) {
|
||||
fMainNtupleManagers.push_back(
|
||||
new G4RootMainNtupleManager(this, fState));
|
||||
}
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4RootNtupleManager::~G4RootNtupleManager()
|
||||
@@ -49,13 +60,55 @@ G4RootNtupleManager::~G4RootNtupleManager()
|
||||
// private methods
|
||||
//
|
||||
|
||||
//_____________________________________________________________________________
|
||||
void G4RootNtupleManager::SetCreateMode()
|
||||
{
|
||||
// Set create mode if not yet defined
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL4() )
|
||||
fState.GetVerboseL4()
|
||||
->Message("set", "ntuple create mode", "");
|
||||
#endif
|
||||
|
||||
G4String createMode;
|
||||
if ( fCreateMode == G4NtupleCreateMode::kUndefined ) {
|
||||
if ( fMainNtupleManagers.size() ) {
|
||||
if ( fFileManager->GetNtupleFile(0) ) {
|
||||
fCreateMode = G4NtupleCreateMode::kMainAfterOpen;
|
||||
createMode = "G4NtupleCreateMode::kMainAfterOpen";
|
||||
} else {
|
||||
fCreateMode = G4NtupleCreateMode::kMainBeforeOpen;
|
||||
createMode = "G4NtupleCreateMode::kMainBeforeOpen";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ( fNtupleDirectory ) {
|
||||
fCreateMode = G4NtupleCreateMode::kNoMergeAfterOpen;
|
||||
createMode = "G4NtupleCreateMode::kNoMergeAfterOpen";
|
||||
} else {
|
||||
fCreateMode = G4NtupleCreateMode::kNoMergeBeforeOpen;
|
||||
createMode = "G4NtupleCreateMode::kNoMergeBeforeOpen";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL2() )
|
||||
fState.GetVerboseL2()
|
||||
->Message("set", "ntuple create mode", createMode);
|
||||
#endif
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
void G4RootNtupleManager::CreateTNtuple(
|
||||
G4TNtupleDescription<tools::wroot::ntuple>* ntupleDescription,
|
||||
const G4String& name, const G4String& title)
|
||||
{
|
||||
// Create ntuple if the file is open
|
||||
if ( fNtupleDirectory ) {
|
||||
// Set create mode if not yet defined
|
||||
SetCreateMode();
|
||||
|
||||
if ( fCreateMode == G4NtupleCreateMode::kNoMergeAfterOpen ) {
|
||||
ntupleDescription->fNtuple
|
||||
= new tools::wroot::ntuple(*fNtupleDirectory, name, title);
|
||||
ntupleDescription->fIsNtupleOwner = false;
|
||||
@@ -68,17 +121,107 @@ void G4RootNtupleManager::CreateTNtuple(
|
||||
void G4RootNtupleManager::CreateTNtupleFromBooking(
|
||||
G4TNtupleDescription<tools::wroot::ntuple>* ntupleDescription)
|
||||
{
|
||||
if ( fCreateMode == G4NtupleCreateMode::kNoMergeBeforeOpen ) {
|
||||
ntupleDescription->fNtuple
|
||||
= new tools::wroot::ntuple(
|
||||
*fNtupleDirectory, ntupleDescription->fNtupleBooking);
|
||||
|
||||
auto basketSize = fFileManager->GetBasketSize();
|
||||
ntupleDescription->fNtuple->set_basket_size(basketSize);
|
||||
|
||||
ntupleDescription->fIsNtupleOwner = false;
|
||||
// ntuple object is deleted automatically when closing a file
|
||||
fNtupleVector.push_back(ntupleDescription->fNtuple);
|
||||
fNtupleVector.push_back(ntupleDescription->fNtuple);
|
||||
}
|
||||
|
||||
if ( fCreateMode == G4NtupleCreateMode::kMainBeforeOpen ) {
|
||||
auto counter = 0;
|
||||
for ( auto manager : fMainNtupleManagers ) {
|
||||
if ( ! manager->GetNtupleVector().size() ) {
|
||||
// Create only once !!
|
||||
manager->SetNtupleFile(fFileManager->GetNtupleFile(counter));
|
||||
manager->SetNtupleDirectory(fFileManager->GetMainNtupleDirectory(counter++));
|
||||
manager->CreateNtuplesFromBooking();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
void G4RootNtupleManager::FinishTNtuple(
|
||||
G4TNtupleDescription<tools::wroot::ntuple>* /*ntupleDescription*/)
|
||||
G4TNtupleDescription<tools::wroot::ntuple>* ntupleDescription)
|
||||
{
|
||||
// nothing to be done here
|
||||
// Create main ntuples
|
||||
|
||||
if ( fCreateMode == G4NtupleCreateMode::kMainAfterOpen ) {
|
||||
auto counter = 0;
|
||||
for ( auto manager : fMainNtupleManagers ) {
|
||||
auto warn = true;
|
||||
manager->SetNtupleFile(fFileManager->GetNtupleFile(counter));
|
||||
manager->SetNtupleDirectory(fFileManager->GetMainNtupleDirectory(counter++));
|
||||
manager->CreateNtuple(ntupleDescription->fNtupleBooking, warn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootNtupleManager::Reset(G4bool deleteNtuple)
|
||||
{
|
||||
G4TNtupleManager<tools::wroot::ntuple> ::Reset(deleteNtuple);
|
||||
// this will clear ntuple vector
|
||||
|
||||
if ( fCreateMode == G4NtupleCreateMode::kNoMergeAfterOpen ) {
|
||||
// clear also ntuple description vector
|
||||
fNtupleDescriptionVector.clear();
|
||||
}
|
||||
|
||||
auto finalResult = true;
|
||||
for ( auto manager : fMainNtupleManagers ) {
|
||||
auto result = manager->Reset(false);
|
||||
finalResult = result && finalResult;
|
||||
}
|
||||
|
||||
return finalResult;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootNtupleManager::Merge()
|
||||
{
|
||||
auto finalResult = true;
|
||||
|
||||
for ( auto manager : fMainNtupleManagers ) {
|
||||
auto result = manager->Merge();
|
||||
finalResult = result && finalResult;
|
||||
}
|
||||
|
||||
return finalResult;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
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;
|
||||
}
|
||||
|
||||
return fMainNtupleManagers[index];
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
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;
|
||||
}
|
||||
|
||||
return fFileManager->GetBasketSize();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,535 @@
|
||||
//
|
||||
// ********************************************************************
|
||||
// * License and Disclaimer *
|
||||
// * *
|
||||
// * The Geant4 software is copyright of the Copyright Holders of *
|
||||
// * the Geant4 Collaboration. It is provided under the terms and *
|
||||
// * conditions of the Geant4 Software License, included in the file *
|
||||
// * LICENSE and available at http://cern.ch/geant4/license . These *
|
||||
// * include a list of copyright holders. *
|
||||
// * *
|
||||
// * Neither the authors of this software system, nor their employing *
|
||||
// * institutes,nor the agencies providing financial support for this *
|
||||
// * work make any representation or warranty, express or implied, *
|
||||
// * regarding this software system or assume any liability for its *
|
||||
// * use. Please see the license in the file LICENSE and URL above *
|
||||
// * for the full disclaimer and the limitation of liability. *
|
||||
// * *
|
||||
// * This code implementation is the result of the scientific and *
|
||||
// * technical work of the GEANT4 collaboration. *
|
||||
// * By using, copying, modifying or distributing the software (or *
|
||||
// * any work based on the software) you agree to acknowledge its *
|
||||
// * use in resulting scientific publications, and indicate your *
|
||||
// * acceptance of all terms of the Geant4 Software license. *
|
||||
// ********************************************************************
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Author: Ivana Hrivnacova, 04/10/2016 (ivana@ipno.in2p3.fr)
|
||||
|
||||
#include "G4RootPNtupleManager.hh"
|
||||
#include "G4RootMainNtupleManager.hh"
|
||||
#include "G4AnalysisUtilities.hh"
|
||||
|
||||
#include "tools/wroot/file"
|
||||
#include "tools/wroot/ntuple"
|
||||
|
||||
// mutex in a file scope
|
||||
namespace {
|
||||
//Mutex to lock master manager when adding ntuple row
|
||||
G4Mutex addRowMutex = G4MUTEX_INITIALIZER;
|
||||
G4Mutex endFillMutex = G4MUTEX_INITIALIZER;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4RootPNtupleManager::G4RootPNtupleManager(G4RootMainNtupleManager* main,
|
||||
const G4AnalysisManagerState& state)
|
||||
: G4VNtupleManager(state),
|
||||
fCreateMode(G4PNtupleCreateMode::kUndefined),
|
||||
fMainNtupleManager(main),
|
||||
fNtupleVector()
|
||||
{}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4RootPNtupleManager::~G4RootPNtupleManager()
|
||||
{
|
||||
for ( auto ntupleDescription : fNtupleDescriptionVector ) {
|
||||
delete ntupleDescription;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// private functions
|
||||
//
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4RootPNtupleDescription*
|
||||
G4RootPNtupleManager::GetNtupleDescriptionInFunction(
|
||||
G4int id, G4String functionName, G4bool warn) const
|
||||
{
|
||||
auto index = id - fFirstId;
|
||||
if ( index < 0 || index >= G4int(fNtupleDescriptionVector.size()) ) {
|
||||
if ( warn) {
|
||||
G4String inFunction = "G4RootPNtupleManager::";
|
||||
inFunction += functionName;
|
||||
G4ExceptionDescription description;
|
||||
description << " " << "ntuple " << id << " does not exist.";
|
||||
G4Exception(inFunction, "Analysis_W011", JustWarning, description);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return fNtupleDescriptionVector[index];
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
tools::wroot::pntuple* G4RootPNtupleManager::GetNtupleInFunction(
|
||||
G4int id, G4String functionName, G4bool warn) const
|
||||
{
|
||||
auto ntupleDescription = GetNtupleDescriptionInFunction(id, functionName);
|
||||
if ( ! ntupleDescription ) return nullptr;
|
||||
|
||||
if ( ! ntupleDescription->fNtuple ) {
|
||||
if ( warn ) {
|
||||
G4String inFunction = "G4RootPNtupleManager::";
|
||||
inFunction += functionName;
|
||||
G4ExceptionDescription description;
|
||||
description << " " << "ntupleId " << id << " does not exist.";
|
||||
G4Exception(inFunction, "Analysis_W011", JustWarning, description);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
return ntupleDescription->fNtuple;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
tools::wroot::ntuple*
|
||||
G4RootPNtupleManager::GetMainNtupleInFunction(
|
||||
G4int id, G4String functionName, G4bool warn) const
|
||||
{
|
||||
auto& mainNtupleVector
|
||||
= fMainNtupleManager->GetNtupleVector();
|
||||
|
||||
auto index = id - fFirstId;
|
||||
if ( index < 0 || index >= G4int(mainNtupleVector.size()) ) {
|
||||
if ( warn) {
|
||||
G4String inFunction = "G4RootPNtupleManager::";
|
||||
inFunction += functionName;
|
||||
G4ExceptionDescription description;
|
||||
description << " " << "main ntuple " << id << " does not exist.";
|
||||
G4Exception(inFunction, "Analysis_W011", JustWarning, description);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return mainNtupleVector[index];
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// protected functions
|
||||
//
|
||||
|
||||
//_____________________________________________________________________________
|
||||
void G4RootPNtupleManager::CreateNtuple(G4RootPNtupleDescription* ntupleDescription,
|
||||
tools::wroot::ntuple* mainNtuple)
|
||||
{
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL4() )
|
||||
fState.GetVerboseL4()
|
||||
->Message("create from main", "pntuple", mainNtuple->name());
|
||||
#endif
|
||||
|
||||
auto rfile = fMainNtupleManager->GetNtupleFile();
|
||||
auto directory = fMainNtupleManager->GetNtupleDirectory();
|
||||
|
||||
// Get parameters from main ntuple
|
||||
ntupleDescription->fFile = rfile.get();
|
||||
mainNtuple->get_branches(ntupleDescription->fMainBranches);
|
||||
|
||||
std::vector<tools::uint32> basketSizes;
|
||||
tools_vforcit(tools::wroot::branch*, ntupleDescription->fMainBranches, it) {
|
||||
basketSizes.push_back((*it)->basket_size());
|
||||
}
|
||||
|
||||
// Create pntuple
|
||||
G4bool verbose = true;
|
||||
ntupleDescription->fNtuple
|
||||
= new tools::wroot::pntuple(
|
||||
G4cout, rfile->byte_swap(), rfile->compression(),
|
||||
directory->seek_directory(), basketSizes,
|
||||
ntupleDescription->fNtupleBooking, verbose);
|
||||
|
||||
ntupleDescription->fIsNtupleOwner = true;
|
||||
// pntuple object is not deleted automatically
|
||||
fNtupleVector.push_back(ntupleDescription->fNtuple);
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL3() )
|
||||
fState.GetVerboseL3()
|
||||
->Message("create from main", "pntuple", mainNtuple->name());
|
||||
#endif
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
void G4RootPNtupleManager::CreateNtuplesFromMain()
|
||||
{
|
||||
// Create ntuple from booking (if not yet done) and main ntuple
|
||||
// This function is called from G4AnalysisManager::OpenFile.
|
||||
|
||||
if ( fCreateMode == G4PNtupleCreateMode::kUndefined ) {
|
||||
if ( fNtupleDescriptionVector.size() ) {
|
||||
fCreateMode = G4PNtupleCreateMode::kSlaveBeforeOpen;
|
||||
} else {
|
||||
fCreateMode = G4PNtupleCreateMode::kSlaveAfterOpen;
|
||||
}
|
||||
}
|
||||
|
||||
if ( fCreateMode == G4PNtupleCreateMode::kSlaveAfterOpen ) {
|
||||
// ntuples are not yet booked
|
||||
return;
|
||||
}
|
||||
|
||||
auto& mainNtupleVector
|
||||
= fMainNtupleManager->GetNtupleVector();
|
||||
|
||||
G4int lcounter = 0;
|
||||
for ( auto mainNtuple : mainNtupleVector ) {
|
||||
|
||||
auto& ntupleDescription
|
||||
= fNtupleDescriptionVector[lcounter++];
|
||||
CreateNtuple(ntupleDescription, mainNtuple);
|
||||
}
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4int G4RootPNtupleManager::CreateNtuple(
|
||||
const G4String& name, const G4String& title)
|
||||
{
|
||||
// Create pntuple description with ntuple_booking
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL4() )
|
||||
fState.GetVerboseL4()->Message("create", "pntuple booking", name);
|
||||
#endif
|
||||
|
||||
// Set create mode if not yet defined
|
||||
if ( fCreateMode == G4PNtupleCreateMode::kUndefined ) {
|
||||
if ( fMainNtupleManager->GetNtupleFile() ) {
|
||||
fCreateMode = G4PNtupleCreateMode::kSlaveAfterOpen;
|
||||
} else {
|
||||
fCreateMode = G4PNtupleCreateMode::kSlaveBeforeOpen;
|
||||
}
|
||||
}
|
||||
|
||||
// Create ntuple description
|
||||
auto index = fNtupleDescriptionVector.size();
|
||||
auto ntupleDescription = new G4RootPNtupleDescription();
|
||||
fNtupleDescriptionVector.push_back(ntupleDescription);
|
||||
|
||||
// Save name & title in ntuple booking
|
||||
ntupleDescription->fNtupleBooking.set_name(name);
|
||||
ntupleDescription->fNtupleBooking.set_title(title);
|
||||
|
||||
fLockFirstId = true;
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL2() ) {
|
||||
G4ExceptionDescription description;
|
||||
description << name << " ntupleId " << index + fFirstId;
|
||||
fState.GetVerboseL2()->Message("create", "pntuple booking", description);
|
||||
}
|
||||
#endif
|
||||
|
||||
return index + fFirstId;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4int G4RootPNtupleManager::CreateNtupleIColumn(
|
||||
const G4String& name, std::vector<int>* vector)
|
||||
{
|
||||
return CreateNtupleTColumn<int>(name, vector);
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4int G4RootPNtupleManager::CreateNtupleFColumn(
|
||||
const G4String& name, std::vector<float>* vector)
|
||||
{
|
||||
return CreateNtupleTColumn<float>(name, vector);
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4int G4RootPNtupleManager::CreateNtupleDColumn(
|
||||
const G4String& name, std::vector<double>* vector)
|
||||
{
|
||||
return CreateNtupleTColumn<double>(name, vector);
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4int G4RootPNtupleManager::CreateNtupleSColumn(
|
||||
const G4String& name)
|
||||
{
|
||||
return CreateNtupleTColumn<std::string>(name, nullptr);
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
void G4RootPNtupleManager::FinishNtuple()
|
||||
{
|
||||
auto ntupleId = fNtupleDescriptionVector.size() + fFirstId - 1;
|
||||
FinishNtuple(ntupleId);
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4int G4RootPNtupleManager::CreateNtupleIColumn(
|
||||
G4int ntupleId, const G4String& name, std::vector<int>* vector)
|
||||
{
|
||||
return CreateNtupleTColumn<int>(ntupleId, name, vector);
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4int G4RootPNtupleManager::CreateNtupleFColumn(
|
||||
G4int ntupleId, const G4String& name, std::vector<float>* vector)
|
||||
{
|
||||
return CreateNtupleTColumn<float>(ntupleId, name, vector);
|
||||
}
|
||||
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4int G4RootPNtupleManager::CreateNtupleDColumn(
|
||||
G4int ntupleId, const G4String& name, std::vector<double>* vector)
|
||||
{
|
||||
return CreateNtupleTColumn<double>(ntupleId, name, vector);
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4int G4RootPNtupleManager::CreateNtupleSColumn(
|
||||
G4int ntupleId, const G4String& name)
|
||||
{
|
||||
return CreateNtupleTColumn<std::string>(ntupleId, name, nullptr);
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
void G4RootPNtupleManager::FinishNtuple(G4int ntupleId)
|
||||
{
|
||||
// create ntuple if file was open
|
||||
|
||||
// if ( fMainNtupleManager->GetNtupleFile() ) {
|
||||
if ( fCreateMode == G4PNtupleCreateMode::kSlaveAfterOpen ) {
|
||||
auto ntupleDescription
|
||||
= GetNtupleDescriptionInFunction(ntupleId, "FinishNtuple");
|
||||
if ( ! ntupleDescription ) return;
|
||||
|
||||
auto mainNtuple = GetMainNtupleInFunction(ntupleId, "FinishNtuple");
|
||||
if ( ! mainNtuple ) return;
|
||||
|
||||
CreateNtuple(ntupleDescription, mainNtuple);
|
||||
}
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootPNtupleManager::FillNtupleIColumn(
|
||||
G4int columnId, G4int value)
|
||||
{
|
||||
return FillNtupleTColumn<int>(columnId, value);
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootPNtupleManager::FillNtupleFColumn(
|
||||
G4int columnId, G4float value)
|
||||
{
|
||||
return FillNtupleTColumn<float>(columnId, value);
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootPNtupleManager::FillNtupleDColumn(
|
||||
G4int columnId, G4double value)
|
||||
{
|
||||
return FillNtupleTColumn<double>(columnId, value);
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootPNtupleManager::FillNtupleSColumn(
|
||||
G4int columnId, const G4String& value)
|
||||
{
|
||||
return FillNtupleTColumn<std::string>(columnId, value);
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootPNtupleManager::AddNtupleRow()
|
||||
{
|
||||
return AddNtupleRow(fFirstId);
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
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 ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
|
||||
//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
|
||||
|
||||
auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "AddNtupleRow");
|
||||
if ( ! ntupleDescription ) return false;
|
||||
|
||||
G4AutoLock lock(&addRowMutex);
|
||||
mutex toolsLock(lock);
|
||||
auto result
|
||||
= ntupleDescription->fNtuple
|
||||
->add_row(toolsLock, *ntupleDescription->fFile, ntupleDescription->fMainBranches);
|
||||
|
||||
if ( ! result ) {
|
||||
G4ExceptionDescription description;
|
||||
description << " " << " ntupleId " << ntupleId
|
||||
<< "adding row has failed.";
|
||||
G4Exception("G4RootPNtupleManager::AddNtupleRow()",
|
||||
"Analysis_W002", JustWarning, description);
|
||||
}
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL3() ) {
|
||||
G4ExceptionDescription description;
|
||||
description << " ntupleId " << ntupleId;
|
||||
fState.GetVerboseL3()->Message("add", "pntuple row", description);
|
||||
}
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootPNtupleManager::Merge()
|
||||
{
|
||||
for ( auto ntupleDescription : fNtupleDescriptionVector) {
|
||||
|
||||
// skip inactivated ntuples
|
||||
if ( ! ntupleDescription->fActivation ) continue;
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL4() ) {
|
||||
fState.GetVerboseL4()
|
||||
->Message("merge", "pntuple", ntupleDescription->fNtupleBooking.name());
|
||||
}
|
||||
#endif
|
||||
|
||||
G4AutoLock lock(&endFillMutex);
|
||||
mutex toolsLock(lock);
|
||||
auto result
|
||||
= ntupleDescription->fNtuple
|
||||
->end_fill(toolsLock, *ntupleDescription->fFile, ntupleDescription->fMainBranches);
|
||||
|
||||
if ( ! result ) {
|
||||
G4ExceptionDescription description;
|
||||
description << " " << " ntuple " << ntupleDescription->fNtupleBooking.name()
|
||||
<< "end fill has failed.";
|
||||
G4Exception("G4RootPNtupleManager::Merge()",
|
||||
"Analysis_W002", JustWarning, description);
|
||||
}
|
||||
|
||||
delete ntupleDescription->fNtuple;
|
||||
ntupleDescription->fNtuple = nullptr;
|
||||
|
||||
#ifdef G4VERBOSE
|
||||
if ( fState.GetVerboseL3() ) {
|
||||
fState.GetVerboseL3()
|
||||
->Message("merge", "pntuple", ntupleDescription->fNtupleBooking.name());
|
||||
}
|
||||
#endif
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootPNtupleManager::Reset(G4bool deleteNtuple)
|
||||
{
|
||||
for ( auto ntupleDescription : fNtupleDescriptionVector ) {
|
||||
if ( deleteNtuple ) {
|
||||
delete ntupleDescription->fNtuple;
|
||||
}
|
||||
ntupleDescription->fNtuple = nullptr;
|
||||
}
|
||||
|
||||
fNtupleVector.clear();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
|
||||
void G4RootPNtupleManager::SetActivation(
|
||||
G4bool activation)
|
||||
{
|
||||
for ( auto ntupleDescription : fNtupleDescriptionVector ) {
|
||||
ntupleDescription->fActivation = activation;
|
||||
}
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
|
||||
void G4RootPNtupleManager::SetActivation(
|
||||
G4int ntupleId, G4bool activation)
|
||||
{
|
||||
auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetActivation");
|
||||
if ( ! ntupleDescription ) return;
|
||||
|
||||
ntupleDescription->fActivation = activation;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootPNtupleManager::GetActivation(
|
||||
G4int ntupleId) const
|
||||
{
|
||||
auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetActivation");
|
||||
if ( ! ntupleDescription ) return false;
|
||||
|
||||
return ntupleDescription->fActivation;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4int G4RootPNtupleManager::GetNofNtuples() const
|
||||
{
|
||||
return fNtupleVector.size();
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________
|
||||
G4bool G4RootPNtupleManager::IsEmpty() const
|
||||
{
|
||||
return ! fNtupleDescriptionVector.size();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user