Import Geant4 11.1.0 source tree

This commit is contained in:
Gabriele Cosmo
2022-12-09 14:43:28 +01:00
parent c07cea1fe0
commit 9f34590941
3810 changed files with 200490 additions and 182326 deletions
@@ -36,7 +36,6 @@
#include "globals.hh"
#include "tools/wroot/ntuple"
#include "tools/histo/hmpi"
#include <memory>
#include <string_view>
@@ -59,7 +58,7 @@ class G4RootAnalysisManager : public G4ToolsAnalysisManager
friend class G4ThreadLocalSingleton<G4RootAnalysisManager>;
public:
virtual ~G4RootAnalysisManager();
~G4RootAnalysisManager() override;
// Static methods
static G4RootAnalysisManager* Instance();
@@ -76,29 +75,19 @@ class G4RootAnalysisManager : public G4ToolsAnalysisManager
std::vector<tools::wroot::ntuple*>::const_iterator EndConstNtuple() const;
// MT/MPI
virtual void SetNtupleMerging(G4bool mergeNtuples,
G4int nofReducedNtupleFiles = 0) override;
virtual void SetNtupleRowWise(G4bool rowWise, G4bool rowMode = true) override;
virtual void SetBasketSize(unsigned int basketSize) override;
virtual void SetBasketEntries(unsigned int basketEntries) override;
protected:
// virtual methods from base class
virtual G4bool OpenFileImpl(const G4String& fileName) override;
virtual G4bool WriteImpl() override;
virtual G4bool CloseFileImpl(G4bool reset) override;
virtual G4bool ResetImpl() override;
virtual G4bool IsOpenFileImpl() const final;
void SetNtupleMerging(G4bool mergeNtuples, G4int nofReducedNtupleFiles = 0) override;
void SetNtupleRowWise(G4bool rowWise, G4bool rowMode = true) override;
void SetBasketSize(unsigned int basketSize) override;
void SetBasketEntries(unsigned int basketEntries) override;
private:
G4RootAnalysisManager();
// Static data members
inline static G4RootAnalysisManager* fgMasterInstance { nullptr };
inline static G4ThreadLocal G4bool fgIsInstance { false };
static constexpr std::string_view fkClass { "G4RootAnalysisManager" };
// data members
// Data members
std::shared_ptr<G4RootFileManager> fFileManager { nullptr };
std::shared_ptr<G4RootNtupleFileManager> fNtupleFileManager { nullptr };
};
@@ -28,13 +28,6 @@
#include "G4RootNtupleFileManager.hh"
#include "G4RootFileManager.hh"
//_____________________________________________________________________________
inline
G4bool G4RootAnalysisManager::IsOpenFileImpl() const
{
return fFileManager->IsOpenFile();
}
//_____________________________________________________________________________
inline
tools::wroot::ntuple* G4RootAnalysisManager::GetNtuple() const
@@ -51,7 +51,7 @@ class G4RootAnalysisReader : public G4ToolsAnalysisReader
friend class G4ThreadLocalSingleton<G4RootAnalysisReader>;
public:
virtual ~G4RootAnalysisReader();
~G4RootAnalysisReader() override;
// Static methods
static G4RootAnalysisReader* Instance();
@@ -65,7 +65,7 @@ class G4RootAnalysisReader : public G4ToolsAnalysisReader
G4RootAnalysisReader();
// // Virtual methods from base class
virtual G4bool CloseFilesImpl(G4bool reset) final;
G4bool CloseFilesImpl(G4bool reset) final;
private:
// Static data members
@@ -24,8 +24,7 @@
// ********************************************************************
//
// Manager class for Root ntuples.
// It implements functions specific to Root ntuples in sequential mode.
// The tuple for root file including its directories
//
// Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
@@ -55,16 +55,17 @@ class G4RootFileManager : public G4VTFileManager<G4RootFile>
public:
explicit G4RootFileManager(const G4AnalysisManagerState& state);
G4RootFileManager() = delete;
virtual ~G4RootFileManager() = default;
~G4RootFileManager() override = default;
using G4BaseFileManager::GetNtupleFileName;
using G4VTFileManager<G4RootFile>::WriteFile;
using G4VTFileManager<G4RootFile>::CloseFile;
// Methods to manipulate file from base classes
virtual G4bool OpenFile(const G4String& fileName) final;
G4bool OpenFile(const G4String& fileName) final;
virtual G4String GetFileType() const final { return "root"; }
G4String GetFileType() const final { return "root"; }
G4bool HasCycles() const final { return true; }
// Specific methods for files per objects
std::shared_ptr<G4RootFile> CreateNtupleFile(RootNtupleDescription* ntupleDescription,
@@ -72,8 +73,8 @@ class G4RootFileManager : public G4VTFileManager<G4RootFile>
std::shared_ptr<G4RootFile> GetNtupleFile(RootNtupleDescription* ntupleDescription,
G4bool perThread = true,
G4int mainNumber = -1) const;
G4bool WriteNtupleFile(RootNtupleDescription* ntupleDescription);
G4bool CloseNtupleFile(RootNtupleDescription* ntupleDescription);
G4bool CloseNtupleFile(RootNtupleDescription* ntupleDescription,
G4int mainNumber = -1);
// Set methods
void SetBasketSize(unsigned int basketSize);
@@ -85,9 +86,9 @@ class G4RootFileManager : public G4VTFileManager<G4RootFile>
protected:
// Methods derived from templated base class
virtual std::shared_ptr<G4RootFile> CreateFileImpl(const G4String& fileName) final;
virtual G4bool WriteFileImpl(std::shared_ptr<G4RootFile> file) final;
virtual G4bool CloseFileImpl(std::shared_ptr<G4RootFile> file) final;
std::shared_ptr<G4RootFile> CreateFileImpl(const G4String& fileName) final;
G4bool WriteFileImpl(std::shared_ptr<G4RootFile> file) final;
G4bool CloseFileImpl(std::shared_ptr<G4RootFile> file) final;
private:
// Methods
@@ -45,13 +45,13 @@ class G4RootHnFileManager : public G4VTHnFileManager<HT>
explicit G4RootHnFileManager(G4RootFileManager* fileManger)
: G4VTHnFileManager<HT>(), fFileManager(fileManger) {}
G4RootHnFileManager() = delete;
virtual ~G4RootHnFileManager() = default;
~G4RootHnFileManager() override = default;
// Methods for writing objects
// Write to a new file (the file is closed after write)
virtual G4bool WriteExtra(HT* ht, const G4String& htName, const G4String& fileName) final;
G4bool WriteExtra(HT* ht, const G4String& htName, const G4String& fileName) final;
// Write to the default file (handled with OpenFile()/CloseFile methods)
virtual G4bool Write(HT* ht, const G4String& htName, G4String& fileName) final;
G4bool Write(HT* ht, const G4String& htName, G4String& fileName) final;
private:
// Methods
@@ -53,11 +53,11 @@ G4bool G4RootHnFileManager<HT>::WriteExtra(
// TO DO
// rfile->add_ziper('Z', toolx::compress_buffer);
// rfile->set_compression(fState.GetCompressionLevel());
if ( ! rfile ) return false;
if (rfile == nullptr) return false;
// no directory supported in this mode
auto hdirectory = &(rfile->dir());
if ( ! hdirectory ) return false;
if (hdirectory == nullptr) return false;
// write histo
result &= Write(hdirectory, ht, htName);
@@ -52,15 +52,15 @@ class G4RootHnRFileManager : public G4VTHnRFileManager<HT>
explicit G4RootHnRFileManager(G4RootRFileManager* rfileManger)
: G4VTHnRFileManager<HT>(), fRFileManager(rfileManger) {}
G4RootHnRFileManager() = delete;
virtual ~G4RootHnRFileManager() = default;
~G4RootHnRFileManager() override = default;
// Methods for reading objects
virtual HT* Read(const G4String& htName, const G4String& fileName,
const G4String& dirName, G4bool isUserFileName) final;
HT* Read(const G4String& htName, const G4String& fileName, const G4String& dirName,
G4bool isUserFileName) final;
private:
// Methods
std::tuple<tools::rroot::buffer*, tools::rroot::TDirectory*> GetBuffer(
tools::rroot::buffer* GetBuffer(
const G4String& fileName,
const G4String& dirName,
const G4String& name);
@@ -27,6 +27,7 @@
// Author: Ivana Hrivnacova, 26/08/2021 (ivana@ipno.in2p3.fr)
#include "G4AnalysisUtilities.hh"
#include "G4RootRFileDef.hh"
#include "tools/rroot/file"
#include "tools/rroot/rall"
@@ -35,8 +36,7 @@
//_____________________________________________________________________________
template <typename HT>
inline
std::tuple<tools::rroot::buffer*, tools::rroot::TDirectory*>
G4RootHnRFileManager<HT>::GetBuffer(
tools::rroot::buffer* G4RootHnRFileManager<HT>::GetBuffer(
const G4String& fileName, const G4String& dirName, const G4String& objectName)
{
// Get buffer for reading histogram or profile specified by objectNmae
@@ -47,53 +47,62 @@ G4RootHnRFileManager<HT>::GetBuffer(
G4bool isPerThread = false;
// Get or open a file
auto rfile = fRFileManager->GetRFile(fileName, isPerThread);
if ( ! rfile ) {
auto rfileTuple = fRFileManager->GetRFile(fileName, isPerThread);
if (rfileTuple == nullptr) {
if ( ! fRFileManager->OpenRFile(fileName, isPerThread) ) {
return std::tuple(nullptr, nullptr);
return nullptr;
}
rfileTuple = fRFileManager->GetRFile(fileName, isPerThread);
}
auto rfile = std::get<0>(*rfileTuple);
// Get or open a directory if dirName is defined
tools::rroot::TDirectory* histoDirectory = nullptr;
if ( histoDirectory == nullptr ) {
// Retrieve directory only once as analysis manager supports only
// 1 histo directory par file)
if ( ! dirName.empty() ) {
histoDirectory = tools::rroot::find_dir(rfile->dir(), dirName);
if ( histoDirectory != nullptr ) {
std::get<1>(*rfileTuple) = histoDirectory;
}
else {
G4Analysis::Warn(
"Directory " + dirName + " not found in file " + fileName + ".",
fkClass, "ReadNtupleImpl");
return nullptr;
}
}
rfile = fRFileManager->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(objectName);
}
else {
G4Analysis::Warn(
"Directory " + dirName + " not found in file " + fileName + ".",
fkClass, "GetBuffer");
return std::tuple(nullptr, nullptr);
}
if ( histoDirectory != nullptr ) {
key = histoDirectory->find_key(objectName);
}
else {
key = rfile->dir().find_key(objectName);
}
if ( ! key ) {
if (key == nullptr) {
G4Analysis::Warn(
"Key " + objectName + " for Histogram/Profile not found in file " +
fileName + ", directory " + dirName, fkClass, "GetBuffer");
return std::tuple(nullptr, newDir);
return nullptr;
}
unsigned int size;
char* charBuffer = key->get_object_buffer(*rfile, size);
if ( ! charBuffer ) {
if (charBuffer == nullptr) {
G4Analysis::Warn(
"Cannot get " + objectName + " in file " + fileName,
fkClass, "GetBuffer");
return std::tuple(nullptr, newDir);
return nullptr;
}
auto verbose = false;
return std::tuple(new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer,
key->key_length(), verbose), newDir);
return new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer,
key->key_length(), verbose);
}
//_____________________________________________________________________________
@@ -148,15 +157,13 @@ HT* G4RootHnRFileManager<HT>::Read(
const G4String& htName, const G4String& fileName, const G4String& dirName,
G4bool /*isUserFileName*/)
{
auto [buffer, newDir] = GetBuffer(fileName, dirName, htName);
auto buffer = GetBuffer(fileName, dirName, htName);
if ( ! buffer ) {
delete newDir;
return nullptr;
}
auto ht = ReadT(buffer);
delete buffer;
delete newDir;
if ( ! ht ) {
G4Analysis::Warn(
@@ -33,13 +33,14 @@
#include "G4BaseAnalysisManager.hh"
#include "G4NtupleBookingManager.hh"
#include "G4TNtupleDescription.hh"
#include "G4RootNtupleManager.hh"
#include "G4RootFileDef.hh"
#include "G4RootNtupleManager.hh"
#include "G4TNtupleDescription.hh"
#include "globals.hh"
#include <vector>
#include <string_view>
#include <utility>
#include <vector>
class G4RootFileManager;
class G4RootNtupleManager;
@@ -53,6 +54,7 @@ class ntuple;
// Types alias
using RootNtupleDescription = G4TNtupleDescription<tools::wroot::ntuple, G4RootFile>;
using RootMainNtupleDescription = std::pair<RootNtupleDescription*, std::shared_ptr<G4RootFile>>;
class G4RootMainNtupleManager : public G4BaseAnalysisManager
{
@@ -66,7 +68,9 @@ class G4RootMainNtupleManager : public G4BaseAnalysisManager
G4bool rowWise, G4int fileNumber,
const G4AnalysisManagerState& state);
G4RootMainNtupleManager() = delete;
virtual ~G4RootMainNtupleManager() = default;
~G4RootMainNtupleManager() override = default;
void CreateNtuplesFromBooking();
protected:
// Methods to manipulate ntuples
@@ -80,6 +84,10 @@ class G4RootMainNtupleManager : public G4BaseAnalysisManager
void SetRowWise(G4bool rowWise);
std::shared_ptr<G4RootFile> GetNtupleFile(RootNtupleDescription* ntupleDescription) const;
// New cycle option
void SetNewCycle(G4bool value);
G4bool GetNewCycle() const;
// Access functions
const std::vector<tools::wroot::ntuple*>& GetNtupleVector()
{ return fNtupleVector; }
@@ -96,12 +104,15 @@ class G4RootMainNtupleManager : public G4BaseAnalysisManager
G4bool fRowWise;
G4int fFileNumber;
std::vector<tools::wroot::ntuple*> fNtupleVector;
std::vector<RootNtupleDescription*> fNtupleDescriptionVector;
std::vector<RootMainNtupleDescription> fNtupleDescriptionVector;
G4bool fNewCycle { false };
};
inline void G4RootMainNtupleManager::SetFileManager(
std::shared_ptr<G4RootFileManager> fileManager)
{ fFileManager = fileManager; }
{
fFileManager = std::move(fileManager);
}
inline void G4RootMainNtupleManager::SetRowWise(G4bool rowWise)
{ fRowWise = rowWise; }
@@ -109,5 +120,10 @@ inline void G4RootMainNtupleManager::SetRowWise(G4bool rowWise)
inline unsigned int G4RootMainNtupleManager::GetBasketEntries() const
{ return fNtupleBuilder->GetBasketEntries(); }
#endif
inline void G4RootMainNtupleManager::SetNewCycle(G4bool value)
{ fNewCycle = value; }
inline G4bool G4RootMainNtupleManager::GetNewCycle() const
{ return fNewCycle; }
#endif
@@ -35,6 +35,7 @@
#include "G4VNtupleFileManager.hh"
#include <string_view>
#include <utility>
class G4RootFileManager;
class G4RootNtupleManager;
@@ -42,12 +43,6 @@ class G4RootPNtupleManager;
class G4VNtupleManager;
class G4NtupleBookingManager;
enum class G4NtupleMergeMode {
kNone,
kMain,
kSlave
};
class G4RootNtupleFileManager : public G4VNtupleFileManager
{
friend class G4RootMpiNtupleFileManager;
@@ -55,27 +50,26 @@ class G4RootNtupleFileManager : public G4VNtupleFileManager
public:
explicit G4RootNtupleFileManager(const G4AnalysisManagerState& state);
G4RootNtupleFileManager() = delete;
virtual ~G4RootNtupleFileManager();
~G4RootNtupleFileManager() override;
// MT/MPI
virtual void SetNtupleMerging(G4bool mergeNtuples,
G4int nofReducedNtupleFiles = 0) override;
virtual void SetNtupleRowWise(G4bool rowWise, G4bool rowMode = true) override;
virtual void SetBasketSize(unsigned int basketSize) override;
virtual void SetBasketEntries(unsigned int basketEntries) override;
void SetNtupleMerging(G4bool mergeNtuples, G4int nofReducedNtupleFiles = 0) override;
void SetNtupleRowWise(G4bool rowWise, G4bool rowMode = true) override;
void SetBasketSize(unsigned int basketSize) override;
void SetBasketEntries(unsigned int basketEntries) override;
// virtual methods from base class
virtual G4bool ActionAtOpenFile(const G4String& fileName) override;
virtual G4bool ActionAtWrite() override;
virtual G4bool ActionAtCloseFile(G4bool reset) override;
virtual G4bool Reset() override;
virtual G4bool IsNtupleMergingSupported() const override;
G4bool ActionAtOpenFile(const G4String& fileName) override;
G4bool ActionAtWrite() override;
G4bool ActionAtCloseFile() override;
G4bool Reset() override;
G4bool IsNtupleMergingSupported() const override;
virtual std::shared_ptr<G4VNtupleManager> CreateNtupleManager() override;
std::shared_ptr<G4VNtupleManager> CreateNtupleManager() override;
void SetFileManager(std::shared_ptr<G4RootFileManager> fileManager);
G4NtupleMergeMode GetMergeMode() const;
G4NtupleMergeMode GetMergeMode() const override;
std::shared_ptr<G4RootNtupleManager> GetNtupleManager() const;
private:
@@ -102,7 +96,9 @@ class G4RootNtupleFileManager : public G4VNtupleFileManager
inline void G4RootNtupleFileManager::SetFileManager(
std::shared_ptr<G4RootFileManager> fileManager)
{ fFileManager = fileManager; }
{
fFileManager = std::move(fileManager);
}
inline G4NtupleMergeMode G4RootNtupleFileManager::GetMergeMode() const
{ return fNtupleMergeMode; }
@@ -70,17 +70,14 @@ class G4RootNtupleManager : public G4TNtupleManager<tools::wroot::ntuple,
G4int nofMainManagers, G4int nofReducedFiles,
G4bool rowWise, G4bool rowMode);
G4RootNtupleManager() = delete;
virtual ~G4RootNtupleManager() = default;
~G4RootNtupleManager() override = default;
private:
private:
// Methods from the templated base class
virtual void CreateTNtupleFromBooking(
RootNtupleDescription* ntupleDescription) final;
virtual void FinishTNtuple(
RootNtupleDescription* ntupleDescription,
G4bool fromBooking) final;
virtual G4bool Reset() final;
virtual void Clear() final;
void CreateTNtupleFromBooking(RootNtupleDescription* ntupleDescription) final;
void FinishTNtuple(RootNtupleDescription* ntupleDescription, G4bool fromBooking) final;
G4bool Reset() final;
void Clear() final;
virtual G4bool Merge();
// Set functions
@@ -88,6 +85,9 @@ class G4RootNtupleManager : public G4TNtupleManager<tools::wroot::ntuple,
void SetNtupleFile(std::shared_ptr<G4RootFile> file);
void SetNtupleRowWise(G4bool rowWise, G4bool rowMode);
// New cycle option
void SetNewCycle(G4bool value) final;
// Access functions
const std::vector<RootNtupleDescription*>& GetNtupleDescriptionVector() const;
std::shared_ptr<G4RootMainNtupleManager> GetMainNtupleManager(G4int index) const;
@@ -56,7 +56,7 @@ inline G4bool G4TNtupleManager<tools::wroot::ntuple, G4RootFile>::FillNtupleTCol
}
auto ntuple = GetNtupleInFunction(ntupleId, "FillNtupleTColumn");
if ( ! ntuple ) return false;
if (ntuple == nullptr) return false;
auto index = columnId - fFirstNtupleColumnId;
if ( index < 0 || index >= G4int(ntuple->columns().size()) ) {
@@ -69,7 +69,7 @@ inline G4bool G4TNtupleManager<tools::wroot::ntuple, G4RootFile>::FillNtupleTCol
auto icolumn = ntuple->columns()[index];
auto column = dynamic_cast<tools::wroot::ntuple::column_string* >(icolumn);
if ( ! column ) {
if (column == nullptr) {
G4Analysis::Warn(
"Column type does not match: ntupleId " + to_string(ntupleId) +
" columnId " + to_string(columnId) + " value " + value,
@@ -50,20 +50,65 @@ class ntuple;
using RootNtupleDescription = G4TNtupleDescription<tools::wroot::ntuple, G4RootFile>;
struct G4RootPNtupleDescription
class G4RootPNtupleDescription
{
G4RootPNtupleDescription(G4NtupleBooking* g4NtupleBooking)
: fDescription(g4NtupleBooking) {}
public:
G4RootPNtupleDescription(G4NtupleBooking* g4NtupleBooking)
: fDescription(g4NtupleBooking) {}
~G4RootPNtupleDescription()
~G4RootPNtupleDescription()
{
if ( fDescription.fIsNtupleOwner ) delete fNtuple;
if ( fDescription.GetIsNtupleOwner() ) delete fNtuple;
}
RootNtupleDescription fDescription;
tools::wroot::imt_ntuple* fNtuple { nullptr };
tools::wroot::base_pntuple* fBasePNtuple { nullptr };
std::vector<tools::wroot::branch*> fMainBranches;
// Set methods
void SetNtuple(tools::wroot::imt_ntuple* intuple);
void SetBasePNtuple(tools::wroot::base_pntuple* basePNtuple);
void Reset();
// Get methods
RootNtupleDescription& GetDescription();
tools::wroot::imt_ntuple* GetNtuple() const;
tools::wroot::base_pntuple* GetBasePNtuple() const;
std::vector<tools::wroot::branch*>& GetMainBranches();
private:
RootNtupleDescription fDescription;
tools::wroot::imt_ntuple* fNtuple { nullptr };
tools::wroot::base_pntuple* fBasePNtuple { nullptr };
std::vector<tools::wroot::branch*> fMainBranches;
};
// inline function
inline void G4RootPNtupleDescription::SetNtuple(
tools::wroot::imt_ntuple* intuple)
{ fNtuple = intuple; }
inline void G4RootPNtupleDescription::SetBasePNtuple(
tools::wroot::base_pntuple* basePNtuple)
{ fBasePNtuple = basePNtuple; }
inline void G4RootPNtupleDescription::Reset()
{
if ( fDescription.GetIsNtupleOwner() ) delete fNtuple;
fNtuple = nullptr;
}
inline RootNtupleDescription&
G4RootPNtupleDescription::GetDescription()
{ return fDescription; }
inline tools::wroot::imt_ntuple*
G4RootPNtupleDescription::GetNtuple() const
{ return fNtuple; }
inline tools::wroot::base_pntuple*
G4RootPNtupleDescription::GetBasePNtuple() const
{ return fBasePNtuple; }
inline std::vector<tools::wroot::branch*>&
G4RootPNtupleDescription::GetMainBranches()
{ return fMainBranches; }
#endif
@@ -32,6 +32,7 @@
#define G4RootPNtupleManager_h 1
#include "G4BaseNtupleManager.hh"
#include "G4RootMainNtupleManager.hh"
#include "G4RootPNtupleDescription.hh"
#include "G4AnalysisManagerState.hh"
#include "G4AnalysisUtilities.hh"
@@ -57,20 +58,25 @@ class imt_ntuple;
class mutex : public virtual tools::wroot::imutex {
using parent = tools::wroot::imutex;
public:
virtual bool lock() {
bool lock() override
{
// G4cout << "!!! Mutex lock" << G4endl;
m_mutex.lock();
return true;}
virtual bool unlock() {
return true;
}
bool unlock() override
{
m_mutex.unlock();
// G4cout << "!!! Mutex unlock" << G4endl;
return true; }
return true;
}
//virtual bool trylock() {return m_mutex.trylock();}
public:
mutex(G4AutoLock& a_mutex):m_mutex(a_mutex){}
virtual ~mutex() = default;
~mutex() override = default;
protected:
mutex(const mutex& a_from):parent(a_from),m_mutex(a_from.m_mutex){}
mutex(const mutex& a_from) = default;
mutex& operator=(const mutex&){return *this;}
protected:
G4AutoLock& m_mutex;
@@ -87,19 +93,19 @@ class G4RootPNtupleManager : public G4BaseNtupleManager
std::shared_ptr<G4RootMainNtupleManager> main,
G4bool rowWise, G4bool rowMode);
G4RootPNtupleManager() = delete;
virtual ~G4RootPNtupleManager();
~G4RootPNtupleManager() override;
private:
// Methods to manipulate ntuples
void CreateNtupleFromMain(G4RootPNtupleDescription* ntupleDescription,
tools::wroot::ntuple* mainNtuple);
// void CreateNtupleFromMain(G4NtupleBooking* g4NtupleBooking,
// tools::wroot::ntuple* mainNtuple);
void CreateNtupleDescriptionsFromBooking();
void CreateNtuplesFromMain();
void CreateNtuplesIfNeeded();
// Methods to create ntuples
//
virtual G4int CreateNtuple(G4NtupleBooking* booking) final;
G4int CreateNtuple(G4NtupleBooking* booking) final;
// Methods to fill ntuples
// Methods for ntuple with id = FirstNtupleId (from base class)
@@ -109,29 +115,35 @@ class G4RootPNtupleManager : public G4BaseNtupleManager
using G4BaseNtupleManager::FillNtupleSColumn;
using G4BaseNtupleManager::AddNtupleRow;
// Methods for ntuple with id > FirstNtupleId (when more ntuples exist)
virtual G4bool FillNtupleIColumn(G4int ntupleId, G4int columnId, G4int value) final;
virtual G4bool FillNtupleFColumn(G4int ntupleId, G4int columnId, G4float value) final;
virtual G4bool FillNtupleDColumn(G4int ntupleId, G4int columnId, G4double value) final;
virtual G4bool FillNtupleSColumn(G4int ntupleId, G4int columnId,
const G4String& value) final;
virtual G4bool AddNtupleRow(G4int ntupleId) final;
G4bool FillNtupleIColumn(G4int ntupleId, G4int columnId, G4int value) final;
G4bool FillNtupleFColumn(G4int ntupleId, G4int columnId, G4float value) final;
G4bool FillNtupleDColumn(G4int ntupleId, G4int columnId, G4double value) final;
G4bool FillNtupleSColumn(G4int ntupleId, G4int columnId, const G4String& value) final;
G4bool AddNtupleRow(G4int ntupleId) final;
virtual G4bool Merge() final;
// Clear all data
virtual void Clear() final;
virtual G4bool Reset();
void Clear() final;
// Activation option
//
virtual void SetActivation(G4bool activation) final;
virtual void SetActivation(G4int ntupleId, G4bool activation) final;
virtual G4bool GetActivation(G4int ntupleId) const final;
void SetActivation(G4bool activation) final;
void SetActivation(G4int ntupleId, G4bool activation) final;
G4bool GetActivation(G4int ntupleId) const final;
// New cycle option
void SetNewCycle(G4bool value) final;
G4bool GetNewCycle() const final;
// Access methods
virtual G4int GetNofNtuples() const final;
G4int GetNofNtuples() const final;
// Set methods
void SetNtupleRowWise(G4bool rowWise, G4bool rowMode);
// List ntuples
G4bool List(std::ostream& output, G4bool onlyIfActive = true) final;
private:
G4RootPNtupleDescription*
GetNtupleDescriptionInFunction(G4int id, std::string_view function, G4bool warn = true) const;
@@ -157,6 +169,7 @@ class G4RootPNtupleManager : public G4BaseNtupleManager
G4bool fRowWise;
G4bool fRowMode;
G4bool fCreateNtuples { true };
G4bool fNewCycle { false };
};
#include "G4RootPNtupleManager.icc"
@@ -33,10 +33,6 @@ template <>
inline G4bool G4RootPNtupleManager::FillNtupleTColumn(
G4int ntupleId, G4int columnId, const std::string& value)
{
if (fCreateNtuples) {
CreateNtuplesFromMain();
}
if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
G4cout << "Skipping FillNtupleIColumn for " << ntupleId << G4endl;
return false;
@@ -48,8 +44,12 @@ inline G4bool G4RootPNtupleManager::FillNtupleTColumn(
" value " + G4Analysis::ToString(value));
}
// Creating ntuples on workers is triggered with the first FillColumn
// or AddRow (in only columns of vector type call)
CreateNtuplesIfNeeded();
auto ntuple = GetNtupleInFunction(ntupleId, "FillNtupleTColumn");
if ( ! ntuple ) return false;
if (ntuple == nullptr) return false;
auto index = columnId - fFirstNtupleColumnId;
if ( index < 0 || index >= G4int(ntuple->columns().size()) ) {
@@ -62,7 +62,7 @@ inline G4bool G4RootPNtupleManager::FillNtupleTColumn(
auto icolumn = ntuple->columns()[index];
auto column = dynamic_cast<tools::wroot::base_pntuple::column_string* >(icolumn);
if ( ! column ) {
if (column == nullptr) {
G4Analysis::Warn(
" Column type does not match: ntupleId " + to_string(ntupleId) +
" columnId " + to_string(columnId) + " value " + value,
@@ -87,9 +87,9 @@ template <typename T>
G4bool G4RootPNtupleManager::FillNtupleTColumn(
G4int ntupleId, G4int columnId, const T& value)
{
if (fCreateNtuples) {
CreateNtuplesFromMain();
}
// Creating ntuples on workers is triggered with the first FillColumn
// or AddRow (in only columns of vector type call)
CreateNtuplesIfNeeded();
if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
G4cout << "Skipping FillNtupleIColumn for " << ntupleId << G4endl;
@@ -0,0 +1,45 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// The tuple for root read file including its directories
//
// Author: Ivana Hrivnacova, 13/07/2022 (ivana@ipno.in2p3.fr)
#ifndef G4RootRFile_h
#define G4RootRFile_h 1
namespace tools {
namespace rroot {
class directory;
class file;
}
}
using G4RootRFile = std::tuple<tools::rroot::file*,
tools::rroot::directory*,
tools::rroot::directory*>;
#endif
@@ -32,44 +32,37 @@
#define G4RootRFileManager_h 1
#include "G4VRFileManager.hh"
#include "G4RootRFileDef.hh"
#include "globals.hh"
#include <map>
#include <string_view>
namespace tools {
namespace rroot {
class file;
//class directory;
}
}
class G4RootRFileManager : public G4VRFileManager
{
public:
explicit G4RootRFileManager(const G4AnalysisManagerState& state);
G4RootRFileManager() = delete;
virtual ~G4RootRFileManager();
~G4RootRFileManager() override;
virtual G4String GetFileType() const final { return "Root"; }
G4String GetFileType() const final { return "Root"; }
// Methods from base class
virtual void CloseFiles() final {}
void CloseFiles() final {}
// Methods to manipulate input files
virtual G4bool OpenRFile(const G4String& fileName,
G4bool isPerThread);
// Get methods
tools::rroot::file* GetRFile(const G4String& fileName,
G4bool isPerThread) const;
G4RootRFile* GetRFile(const G4String& fileName, G4bool isPerThread) const;
private:
// Static data members
static constexpr std::string_view fkClass { "G4RootRFileManager" };
// data members
std::map<G4String, tools::rroot::file*> fRFiles;
std::map<G4String, G4RootRFile*> fRFiles;
};
#endif
@@ -38,6 +38,7 @@
#include "tools/rroot/ntuple"
#include <string_view>
#include <utility>
class G4RootRFileManager;
struct G4RootRNtupleDescription;
@@ -49,16 +50,16 @@ class G4RootRNtupleManager : public G4TRNtupleManager<tools::rroot::ntuple>
public:
explicit G4RootRNtupleManager(const G4AnalysisManagerState& state);
G4RootRNtupleManager() = delete;
virtual ~G4RootRNtupleManager() = default;
~G4RootRNtupleManager() override = default;
private:
// Set methods
void SetFileManager(std::shared_ptr<G4RootRFileManager> fileManager);
// Methods from the base class
virtual G4int ReadNtupleImpl(const G4String& ntupleName, const G4String& fileName,
const G4String& dirName, G4bool isUserFileName) final;
virtual G4bool GetTNtupleRow(G4TRNtupleDescription<tools::rroot::ntuple>* ntupleDescription) final;
G4int ReadNtupleImpl(const G4String& ntupleName, const G4String& fileName,
const G4String& dirName, G4bool isUserFileName) final;
G4bool GetTNtupleRow(G4TRNtupleDescription<tools::rroot::ntuple>* ntupleDescription) final;
// Static data members
static constexpr std::string_view fkClass { "G4RootPNtupleManager" };
@@ -69,7 +70,9 @@ class G4RootRNtupleManager : public G4TRNtupleManager<tools::rroot::ntuple>
inline void
G4RootRNtupleManager::SetFileManager(std::shared_ptr<G4RootRFileManager> fileManager)
{ fFileManager = fileManager; }
{
fFileManager = std::move(fileManager);
}
#endif
+1
View File
@@ -20,6 +20,7 @@ geant4_add_module(G4root
G4RootPNtupleDescription.hh
G4RootPNtupleManager.hh
G4RootPNtupleManager.icc
G4RootRFileDef.hh
G4RootRFileManager.hh
G4RootRNtupleManager.hh
g4root_defs.hh
@@ -54,14 +54,13 @@ G4bool G4RootAnalysisManager::IsInstance()
G4RootAnalysisManager::G4RootAnalysisManager()
: G4ToolsAnalysisManager("Root")
{
if ( ! G4Threading::IsWorkerThread() ) fgMasterInstance = this;
// File manager
fFileManager = std::make_shared<G4RootFileManager>(fState);
SetFileManager(fFileManager);
// Ntuple file manager
fNtupleFileManager = std::make_shared<G4RootNtupleFileManager>(fState);
SetNtupleFileManager(fNtupleFileManager);
fNtupleFileManager->SetFileManager(fFileManager);
fNtupleFileManager->SetBookingManager(fNtupleBookingManager);
}
@@ -69,135 +68,9 @@ G4RootAnalysisManager::G4RootAnalysisManager()
//_____________________________________________________________________________
G4RootAnalysisManager::~G4RootAnalysisManager()
{
if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
fgIsInstance = false;
}
//
// protected methods
//
//_____________________________________________________________________________
G4bool G4RootAnalysisManager::OpenFileImpl(const G4String& fileName)
{
// Create ntuple manager(s)
// and set it to base class which takes then their ownership
SetNtupleManager(fNtupleFileManager->CreateNtupleManager());
auto result = true;
// Open file
if ( fNtupleFileManager->GetMergeMode() != G4NtupleMergeMode::kSlave ) {
result &= fFileManager->OpenFile(fileName);
}
// Open ntuple file(s) and create ntuples from bookings
result &= fNtupleFileManager->ActionAtOpenFile(fFileManager->GetFullFileName());
return result;
}
//_____________________________________________________________________________
G4bool G4RootAnalysisManager::WriteImpl()
{
auto result = true;
Message(kVL4, "write", "files");
if ( G4Threading::IsWorkerThread() ) {
result &= G4ToolsAnalysisManager::Merge();
}
else {
// Open all files registered with objects
fFileManager->OpenFiles();
// Write all histograms/profile on master
result &= G4ToolsAnalysisManager::WriteImpl();
}
// Ntuples
result &= fNtupleFileManager->ActionAtWrite();
// File
if ( fNtupleFileManager->GetMergeMode() != G4NtupleMergeMode::kSlave ) {
// write all open files
result &= fFileManager->WriteFiles();
}
// Write ASCII if activated
if ( IsAscii() ) {
result &= WriteAscii(fFileManager->GetFileName());
}
Message(kVL3, "write", "files", "", result);
return result;
}
//_____________________________________________________________________________
G4bool G4RootAnalysisManager::CloseFileImpl(G4bool reset)
{
auto result = true;
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();
}
// No files clean-up in sequential mode
if ( ! G4Threading::IsMultithreadedApplication() ) return result;
G4bool isNtupleManagerEmpty = fNtupleBookingManager->IsEmpty();
// Delete files if empty in MT mode
if ( ( fState.GetIsMaster() && G4ToolsAnalysisManager::IsEmpty() &&
isNtupleManagerEmpty ) ||
( (! fState.GetIsMaster()) && isNtupleManagerEmpty &&
fNtupleFileManager->GetMergeMode() == G4NtupleMergeMode::kNone ) ) {
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 {
Message(kVL3, "close", "files");
}
return result;
}
//_____________________________________________________________________________
G4bool G4RootAnalysisManager::ResetImpl()
{
// Reset histograms and ntuple
auto result = true;
result &= G4ToolsAnalysisManager::ResetImpl();
if ( fNtupleFileManager != nullptr ) {
result &= fNtupleFileManager->Reset();
}
return result;
}
//
// public methods
//
+25 -26
View File
@@ -59,7 +59,7 @@ tools::wroot::directory* G4RootFileManager::CreateDirectory(
tools::wroot::file* rfile,
const G4String& directoryName, [[maybe_unused]] const G4String& objectType) const
{
if ( ! rfile ) return nullptr;
if (rfile == nullptr) return nullptr;
if ( directoryName == "" ) {
// Do not create a new directory if its name is not set
@@ -69,13 +69,11 @@ tools::wroot::directory* G4RootFileManager::CreateDirectory(
Message(kVL4, "create", "directory for " + objectType, directoryName);
auto directory = rfile->dir().mkdir(directoryName);
if ( ! directory ) {
if (directory == nullptr) {
Warn("Cannot create directory " + directoryName, fkClass, "CreateDirectory");
return nullptr;
}
else {
Message(kVL2, "create", "directory for " + objectType, directoryName);
}
Message(kVL2, "create", "directory for " + objectType, directoryName);
return directory;
}
@@ -88,12 +86,13 @@ G4String G4RootFileManager::GetNtupleFileName(
{
// get ntuple file name
auto ntupleFileName = ntupleDescription->fFileName;
if ( ntupleFileName.size() ) {
auto ntupleFileName = ntupleDescription->GetFileName();
if (ntupleFileName.size() != 0u) {
if ( perThread ) {
ntupleFileName = GetTnFileName(ntupleFileName, GetFileType());
}
} else {
}
else {
// get default file name
ntupleFileName = GetFullFileName(fFileName, perThread);
}
@@ -129,7 +128,7 @@ G4RootFileManager::CreateFileImpl(const G4String& fileName)
// create histo directory
tools::wroot::directory* hdirectory
= CreateDirectory(file.get(), fHistoDirectoryName, "histograms");
if ( ! hdirectory ) {
if (hdirectory == nullptr) {
// Warning is issued in CreateDirectory
return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr);
}
@@ -137,7 +136,7 @@ G4RootFileManager::CreateFileImpl(const G4String& fileName)
// create ntuple directory
tools::wroot::directory* ndirectory
= CreateDirectory(file.get(), fNtupleDirectoryName, "ntuples");
if ( ! ndirectory ) {
if (ndirectory == nullptr) {
// Warning is issued in CreateDirectory
return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr);
}
@@ -150,10 +149,9 @@ G4bool G4RootFileManager::WriteFileImpl(std::shared_ptr<G4RootFile> file)
{
// New prototype: called by G4TFileManager base classe
if ( ! file ) return false;
// nothing to be done, but file should exist
return (file == nullptr) ? false : true;
unsigned int n;
return std::get<0>(*file)->write(n);
}
//_____________________________________________________________________________
@@ -161,7 +159,11 @@ G4bool G4RootFileManager::CloseFileImpl(std::shared_ptr<G4RootFile> file)
{
// New prototype: called by G4TFileManager base classe
if ( ! file ) return false;
if (file == nullptr) return false;
// write file (only once)
unsigned int n;
std::get<0>(*file)->write(n);
// close file
std::get<0>(*file)->close();
@@ -215,7 +217,7 @@ std::shared_ptr<G4RootFile> G4RootFileManager::CreateNtupleFile(
// register file in ntuple description only if it is not main ntuple file
// (main ntuple files are managed in main ntuple manager)
if ( mainNumber == -1 ) {
ntupleDescription->fFile = file;
ntupleDescription->SetFile(file);
}
return file;
@@ -233,19 +235,16 @@ std::shared_ptr<G4RootFile> G4RootFileManager::GetNtupleFile(
//_____________________________________________________________________________
G4bool G4RootFileManager::CloseNtupleFile(
RootNtupleDescription* ntupleDescription)
RootNtupleDescription* ntupleDescription, G4int mainNumber)
{
auto result = true;
// auto result = true;
if ( ntupleDescription->fFile ) {
// Ntuple files are registered in file manager map.
// they will be closed with CloseFiles() calls
ntupleDescription->fFile.reset();
// Notify not empty file
auto ntupleFileName = GetNtupleFileName(ntupleDescription);
result &= SetIsEmpty(ntupleFileName, ! ntupleDescription->fHasFill);
}
// Notify not empty file
auto ntupleFileName = GetNtupleFileName(ntupleDescription, true, mainNumber);
auto result = SetIsEmpty(ntupleFileName, ! ntupleDescription->GetHasFill());
// Ntuple files will be closed with CloseFiles() calls
ntupleDescription->GetFile().reset();
return result;
}
@@ -62,17 +62,17 @@ void G4RootMainNtupleManager::CreateNtuple(RootNtupleDescription* ntupleDescript
// Get/Create main ntuple file
auto ntupleFile = fFileManager->CreateNtupleFile(ntupleDescription, fFileNumber);
if ( ! ntupleFile ) {
if ( ntupleFile == nullptr ) {
if ( warn ) {
Warn("Ntuple file must be defined first.\n"
"Cannot create main ntuple.",
fkClass, "CreateNtuple");
}
Warn("Ntuple file must be defined first.\n"
"Cannot create main ntuple.",
fkClass, "CreateNtuple");
}
return;
}
// Get ntuple booking
auto ntupleBooking = ntupleDescription->fNtupleBooking;
const auto& ntupleBooking = ntupleDescription->GetNtupleBooking();
Message(kVL4, "create", "main ntuple", ntupleBooking.name());
@@ -83,7 +83,7 @@ void G4RootMainNtupleManager::CreateNtuple(RootNtupleDescription* ntupleDescript
ntuple->set_basket_size(basketSize);
fNtupleVector.push_back(ntuple);
fNtupleDescriptionVector.push_back(ntupleDescription);
fNtupleDescriptionVector.push_back(std::make_pair(ntupleDescription, ntupleFile));
Message(kVL3, "create", "main ntuple", ntupleBooking.name());
}
@@ -97,9 +97,9 @@ G4bool G4RootMainNtupleManager::Merge()
ntuple->merge_number_of_entries();
// Notify ntuple description that file is not empty
if ( ntuple->entries() ) {
auto ntupleDescription = fNtupleDescriptionVector.at(counter);
ntupleDescription->fHasFill = true;
if (ntuple->entries() != 0u) {
auto ntupleDescription = fNtupleDescriptionVector.at(counter).first;
ntupleDescription->SetHasFill(true);
}
++counter;
}
@@ -110,10 +110,10 @@ G4bool G4RootMainNtupleManager::Merge()
//_____________________________________________________________________________
G4bool G4RootMainNtupleManager::Reset()
{
// ntuple object is deleted automatically when closing a file
// The ntuples will be recreated with new cycle or new open file.
// Ntuple objects are deleted automatically when closing a file
fNtupleVector.clear();
fNtupleDescriptionVector.clear();
return true;
}
@@ -121,8 +121,8 @@ G4bool G4RootMainNtupleManager::Reset()
//_____________________________________________________________________________
void G4RootMainNtupleManager::ClearData()
{
// Reset function clears all data
Reset();
fNtupleDescriptionVector.clear();
fNtupleVector.clear();
Message(G4Analysis::kVL2, "clear", "main ntuples");
}
@@ -134,3 +134,34 @@ G4RootMainNtupleManager::GetNtupleFile(RootNtupleDescription* ntupleDescription)
auto perThread = false;
return fFileManager->GetNtupleFile(ntupleDescription, perThread, fFileNumber);
}
//
// public functions
//
//_____________________________________________________________________________
void G4RootMainNtupleManager::CreateNtuplesFromBooking()
{
// Create ntuples from booking.
// This function is triggered from workers at new cycle.
for (auto [ntupleDescription, ntupleFile] : fNtupleDescriptionVector) {
// Get ntuple booking
const auto& ntupleBooking = ntupleDescription->GetNtupleBooking();
Message(kVL4, "create", "main ntuple", ntupleBooking.name());
// Create ntuple
auto ntuple = new tools::wroot::ntuple(*std::get<2>(*ntupleFile), ntupleBooking, fRowWise);
// ntuple object is deleted automatically when closing a file
auto basketSize = fNtupleBuilder->GetBasketSize();
ntuple->set_basket_size(basketSize);
fNtupleVector.push_back(ntuple);
Message(kVL3, "create", "main ntuple", ntupleBooking.name());
}
SetNewCycle(false);
}
@@ -87,8 +87,7 @@ void G4RootNtupleFileManager::SetNtupleMergingMode(G4bool mergeNtuples,
}
// Illegal situations
if ( mergeNtuples && G4Threading::IsMultithreadedApplication() &&
( ! fgMasterInstance ) ) {
if (mergeNtuples && G4Threading::IsMultithreadedApplication() && (fgMasterInstance == nullptr)) {
Warn("Merging ntuples requires G4AnalysisManager instance on master.\n"
"Setting was ignored.",
fkClass, "SetNtupleMergingMode");
@@ -115,6 +114,7 @@ void G4RootNtupleFileManager::SetNtupleMergingMode(G4bool mergeNtuples,
// Forced merging mode
G4bool isMaster = ! G4Threading::IsWorkerThread();
// G4bool isMaster = fState.GetIsMaster();
if ( isMaster ) {
fNtupleMergeMode = G4NtupleMergeMode::kMain;
mergingMode = "G4NtupleMergeMode::kMain";
@@ -130,10 +130,10 @@ void G4RootNtupleFileManager::SetNtupleMergingMode(G4bool mergeNtuples,
//_____________________________________________________________________________
G4int G4RootNtupleFileManager::GetNtupleFileNumber()
{
if ( ! fNofNtupleFiles ) return 0;
if (fNofNtupleFiles == 0) return 0;
G4int nofMainManagers = fNofNtupleFiles;
if ( ! nofMainManagers ) nofMainManagers = 1;
if (nofMainManagers == 0) nofMainManagers = 1;
auto fileNumber = G4Threading::G4GetThreadId() % nofMainManagers;
return fileNumber;
@@ -142,14 +142,16 @@ G4int G4RootNtupleFileManager::GetNtupleFileNumber()
//_____________________________________________________________________________
G4bool G4RootNtupleFileManager::CloseNtupleFiles()
{
// Close ntuple files
// Take into account main ntuple files if present
auto mainNumber = ( fNofNtupleFiles > 0 ) ? 0 : -1;
auto result = true;
auto ntupleVector = fNtupleManager->GetNtupleDescriptionVector();
for ( auto ntupleDescription : ntupleVector) {
result &= fFileManager->CloseNtupleFile(ntupleDescription);
for (G4int i = mainNumber; i < fNofNtupleFiles; ++i ) {
result &= fFileManager->CloseNtupleFile(ntupleDescription, i);
}
}
return result;
}
@@ -243,8 +245,10 @@ std::shared_ptr<G4VNtupleManager> G4RootNtupleFileManager::CreateNtupleManager()
case G4NtupleMergeMode::kMain: {
G4int nofMainManagers = fNofNtupleFiles;
if ( ! nofMainManagers ) nofMainManagers = 1;
// create one manager if merging required into the histos & profiles files
if (nofMainManagers == 0) {
// create one manager if merging required into the histos & profiles files
nofMainManagers = 1;
}
fNtupleManager
= make_shared<G4RootNtupleManager>(
fState, fBookingManager, nofMainManagers, fNofNtupleFiles, fNtupleRowWise, fNtupleRowMode);
@@ -343,24 +347,14 @@ G4bool G4RootNtupleFileManager::ActionAtWrite()
}
//_____________________________________________________________________________
G4bool G4RootNtupleFileManager::ActionAtCloseFile(G4bool reset)
G4bool G4RootNtupleFileManager::ActionAtCloseFile()
{
auto result = true;
// close files
if ( fNtupleMergeMode != G4NtupleMergeMode::kSlave ) {
result &= CloseNtupleFiles();
if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave) {
fSlaveNtupleManager->SetNewCycle(false);
return true;
}
if ( ! reset ) {
// The ntuples must be always reset when closing file)
if ( ! Reset() ) {
Warn("Resetting data failed", fkClass, "ActionAtCloseFile");
result = false;
}
}
return result;
return CloseNtupleFiles();
}
//_____________________________________________________________________________
@@ -375,5 +369,9 @@ G4bool G4RootNtupleFileManager::Reset()
result &= fNtupleManager->Reset();
}
if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave ) {
fSlaveNtupleManager->Reset();
}
return result;
}
@@ -66,9 +66,9 @@ G4RootNtupleManager::G4RootNtupleManager(const G4AnalysisManagerState& state,
void G4RootNtupleManager::CreateTNtupleFromBooking(
RootNtupleDescription* ntupleDescription)
{
if ( ! fMainNtupleManagers.size() ) {
if (fMainNtupleManagers.size() == 0u) {
// No merging
if ( ntupleDescription->fNtuple ) {
if (ntupleDescription->GetNtuple() != nullptr) {
Warn("Cannot create ntuple. Ntuple already exists.",
fkClass, "CreateTNtupleFromBooking");
return;
@@ -83,16 +83,16 @@ void G4RootNtupleManager::CreateTNtupleFromBooking(
}
auto directory = std::get<2>(*ntupleFile);
ntupleDescription->fNtuple
= new tools::wroot::ntuple(
*directory, ntupleDescription->fNtupleBooking, fRowWise);
ntupleDescription->SetNtuple(
new tools::wroot::ntuple(
*directory, ntupleDescription->GetNtupleBooking(), fRowWise));
auto basketSize = fFileManager->GetBasketSize();
ntupleDescription->fNtuple->set_basket_size(basketSize);
ntupleDescription->GetNtuple()->set_basket_size(basketSize);
ntupleDescription->fIsNtupleOwner = false;
ntupleDescription->SetIsNtupleOwner(false);
// ntuple object is deleted automatically when closing a file
fNtupleVector.push_back(ntupleDescription->fNtuple);
fNtupleVector.push_back(ntupleDescription->GetNtuple());
}
else {
// Merging activated
@@ -171,6 +171,16 @@ void G4RootNtupleManager::SetNtupleRowWise(G4bool rowWise, G4bool rowMode)
}
}
//_____________________________________________________________________________
void G4RootNtupleManager::SetNewCycle(G4bool value)
{
G4TNtupleManager<tools::wroot::ntuple, G4RootFile>::SetNewCycle(value);
for ( const auto& manager : fMainNtupleManagers ) {
manager->SetNewCycle(value);
}
}
//_____________________________________________________________________________
std::shared_ptr<G4RootMainNtupleManager>
G4RootNtupleManager::GetMainNtupleManager(G4int index) const
+141 -50
View File
@@ -28,7 +28,6 @@
#include "G4NtupleBookingManager.hh"
#include "G4RootPNtupleManager.hh"
#include "G4RootMainNtupleManager.hh"
#include "G4AnalysisUtilities.hh"
#include "tools/wroot/file"
@@ -42,6 +41,8 @@ namespace {
//Mutex to lock master manager when adding ntuple row and ending fill
G4Mutex pntupleMutex = G4MUTEX_INITIALIZER;
//Mutex to lock master manager when createing main ntuples at new cycle
G4Mutex createMainMutex = G4MUTEX_INITIALIZER;
//_____________________________________________________________________________
void NotExistWarning(const G4String& what, G4int id,
@@ -101,15 +102,15 @@ tools::wroot::base_pntuple*
G4int id, std::string_view functionName, G4bool warn) const
{
auto ntupleDescription = GetNtupleDescriptionInFunction(id, functionName);
if ( ! ntupleDescription ) return nullptr;
if (ntupleDescription == nullptr) return nullptr;
if ( ! ntupleDescription->fBasePNtuple ) {
if (ntupleDescription->GetBasePNtuple() == nullptr) {
if ( warn ) {
NotExistWarning("ntuple", id, fkClass, functionName);
}
return nullptr;
}
return ntupleDescription->fBasePNtuple;
return ntupleDescription->GetBasePNtuple();
}
//_____________________________________________________________________________
@@ -141,17 +142,17 @@ void G4RootPNtupleManager::CreateNtupleFromMain(
{
Message(kVL4, "create from main", "pntuple", mainNtuple->name());
auto file = fMainNtupleManager->GetNtupleFile(&ntupleDescription->fDescription);
auto file = fMainNtupleManager->GetNtupleFile(&ntupleDescription->GetDescription());
if ( ! file ) {
Warn("Cannot create pntuple. Main ntuple file does not exist.",
fkClass, "CreateNtupleFromMain");
return;
}
ntupleDescription->fDescription.fFile = file;
ntupleDescription->GetDescription().SetFile(file);
// Get parameters from ntupleDescription
mainNtuple->get_branches(ntupleDescription->fMainBranches);
mainNtuple->get_branches(ntupleDescription->GetMainBranches());
auto rfile = std::get<0>(*file);
G4bool verbose = true;
@@ -162,16 +163,16 @@ void G4RootPNtupleManager::CreateNtupleFromMain(
G4cout, rfile->byte_swap(), rfile->compression(),
mainNtuple->dir().seek_directory(),
*mainBranch, mainBranch->basket_size(),
ntupleDescription->fDescription.fNtupleBooking, verbose);
ntupleDescription->GetDescription().GetNtupleBooking(), verbose);
ntupleDescription->fNtuple
= static_cast<tools::wroot::imt_ntuple*>(mtNtuple);
ntupleDescription->fBasePNtuple
= static_cast<tools::wroot::base_pntuple*>(mtNtuple);
ntupleDescription->SetNtuple(
static_cast<tools::wroot::imt_ntuple*>(mtNtuple));
ntupleDescription->SetBasePNtuple(
static_cast<tools::wroot::base_pntuple*>(mtNtuple));
}
else {
std::vector<tools::uint32> basketSizes;
tools_vforcit(tools::wroot::branch*, ntupleDescription->fMainBranches, it) {
tools_vforcit(tools::wroot::branch*, ntupleDescription->GetMainBranches(), it) {
basketSizes.push_back((*it)->basket_size());
}
auto basketEntries = fMainNtupleManager->GetBasketEntries();
@@ -180,36 +181,44 @@ void G4RootPNtupleManager::CreateNtupleFromMain(
new tools::wroot::mt_ntuple_column_wise(
G4cout, rfile->byte_swap(), rfile->compression(),
mainNtuple->dir().seek_directory(),
ntupleDescription->fMainBranches, basketSizes,
ntupleDescription->fDescription.fNtupleBooking,
ntupleDescription->GetMainBranches(), basketSizes,
ntupleDescription->GetDescription().GetNtupleBooking(),
fRowMode, basketEntries, verbose);
ntupleDescription->fNtuple
= static_cast<tools::wroot::imt_ntuple*>(mtNtuple);
ntupleDescription->fBasePNtuple
= static_cast<tools::wroot::base_pntuple*>(mtNtuple);
ntupleDescription->SetNtuple(
static_cast<tools::wroot::imt_ntuple*>(mtNtuple));
ntupleDescription->SetBasePNtuple(
static_cast<tools::wroot::base_pntuple*>(mtNtuple));
}
ntupleDescription->fDescription.fIsNtupleOwner = true;
ntupleDescription->GetDescription().SetIsNtupleOwner(true);
// // pntuple object is not deleted automatically
fNtupleVector.push_back(ntupleDescription->fNtuple);
fNtupleVector.push_back(ntupleDescription->GetNtuple());
Message(kVL3, "create from main", "pntuple", mainNtuple->name());
}
//_____________________________________________________________________________
void G4RootPNtupleManager::CreateNtuplesFromMain()
void G4RootPNtupleManager::CreateNtupleDescriptionsFromBooking()
{
// Create ntuple from booking (if not yet done) and main ntuple
// Create ntuple descriptions from booking.
// This function is called from the first Fill call.
// Create pntuple descriptions from ntuple booking.
auto g4NtupleBookings = fBookingManager->GetNtupleBookingVector();
for ( auto g4NtupleBooking : g4NtupleBookings ) {
auto ntupleDescription = new G4RootPNtupleDescription(g4NtupleBooking);
// Save g4booking, activation in pntuple booking
fNtupleDescriptionVector.push_back(ntupleDescription);
}
}
//_____________________________________________________________________________
void G4RootPNtupleManager::CreateNtuplesFromMain()
{
// Create slave ntuples from main ntuple.
// This function is called from the first Fill call.
auto& mainNtupleVector = fMainNtupleManager->GetNtupleVector();
@@ -218,8 +227,52 @@ void G4RootPNtupleManager::CreateNtuplesFromMain()
auto& ntupleDescription = fNtupleDescriptionVector[lcounter++];
CreateNtupleFromMain(ntupleDescription, mainNtuple);
}
}
fCreateNtuples = false;
//_____________________________________________________________________________
void G4RootPNtupleManager::CreateNtuplesIfNeeded()
{
// The ntuples on workers are created at first FillColumn or AddRow
// (if only columns of vector type) call.
// When writing multiple times in teh same file, the main ntuples have
// to be recreated as well.
// G4cout << "G4RootPNtupleManager::CreateNtuplesIfNeeded: "
// << " fCreateNtuple: " << fCreateNtuples << " "
// << " fNewCycle: " << fNewCycle
// << " fMainNtupleManager->GetNewCycle(): " << fMainNtupleManager->GetNewCycle()
// << " fNtupleDescriptionVector.size(): " << fNtupleDescriptionVector.size()
// << " fNtupleVector.size(): " << fNtupleVector.size()
// << G4endl;
if (fCreateNtuples) {
// create ntuple descriptions
CreateNtupleDescriptionsFromBooking();
// create main ntuples if needed
G4AutoLock lock(&createMainMutex);
if (fMainNtupleManager->GetNewCycle()) {
fMainNtupleManager->CreateNtuplesFromBooking();
}
lock.unlock();
// create slave ntuples
CreateNtuplesFromMain();
fCreateNtuples = false;
}
if (fNewCycle) {
// create main ntuples if needed
G4AutoLock lock(&createMainMutex);
if (fMainNtupleManager->GetNewCycle()) {
fMainNtupleManager->CreateNtuplesFromBooking();
}
lock.unlock();
// create slave ntuples
CreateNtuplesFromMain();
fNewCycle = false;
}
}
//_____________________________________________________________________________
@@ -262,10 +315,6 @@ G4bool G4RootPNtupleManager::FillNtupleSColumn(
//_____________________________________________________________________________
G4bool G4RootPNtupleManager::AddNtupleRow(G4int ntupleId)
{
if (fCreateNtuples) {
CreateNtuplesFromMain();
}
if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
//G4cout << "Skipping AddNtupleRow for " << ntupleId << G4endl;
return false;
@@ -275,23 +324,27 @@ G4bool G4RootPNtupleManager::AddNtupleRow(G4int ntupleId)
Message(kVL4, "add", "pntuple row", " ntupleId " + to_string(ntupleId));
}
auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "AddNtupleRow");
if ( ! ntupleDescription ) return false;
// Creating ntuples on workers is triggered with the first FillColumn
// or AddRow (in only columns of vector type call)
CreateNtuplesIfNeeded();
auto rfile = std::get<0>(*ntupleDescription->fDescription.fFile);
auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "AddNtupleRow");
if (ntupleDescription == nullptr) return false;
auto rfile = std::get<0>(*ntupleDescription->GetDescription().GetFile());
G4AutoLock lock(&pntupleMutex);
lock.unlock();
mutex toolsLock(lock);
auto result
= ntupleDescription->fNtuple->add_row(toolsLock, *rfile);
= ntupleDescription->GetNtuple()->add_row(toolsLock, *rfile);
if ( ! result ) {
Warn("NtupleId " + to_string(ntupleId) + "adding row failed.",
fkClass, "AddNtupleRow");
}
ntupleDescription->fDescription.fHasFill = true;
ntupleDescription->GetDescription().SetHasFill(true);
if ( IsVerbose(kVL3) ) {
Message(kVL3, "add", "pntuple row", " ntupleId " + to_string(ntupleId));
@@ -307,45 +360,64 @@ G4bool G4RootPNtupleManager::Merge()
for ( auto ntupleDescription : fNtupleDescriptionVector) {
// skip inactivated ntuples
if(!ntupleDescription->fDescription.fActivation || !ntupleDescription->fNtuple) {
if (! ntupleDescription->GetDescription().GetActivation() ||
(ntupleDescription->GetNtuple() == nullptr)) {
// G4cout << "skipping inactive ntuple " << G4endl;
continue;
}
if ( IsVerbose(kVL4) ) {
Message(kVL4, "merge", "pntuple", ntupleDescription->fDescription.fNtupleBooking.name());
Message(kVL4, "merge", "pntuple",
ntupleDescription->GetDescription().GetNtupleBooking().name());
}
auto rfile = std::get<0>(*ntupleDescription->fDescription.fFile);
auto rfile = std::get<0>(*ntupleDescription->GetDescription().GetFile());
G4AutoLock lock(&pntupleMutex);
lock.unlock();
mutex toolsLock(lock);
auto result
= ntupleDescription->fNtuple->end_fill(toolsLock, *rfile);
= ntupleDescription->GetNtuple()->end_fill(toolsLock, *rfile);
if ( ! result ) {
Warn("Ntuple " + ntupleDescription->fDescription.fNtupleBooking.name() +
Warn("Ntuple " + ntupleDescription->GetDescription().GetNtupleBooking().name() +
"end fill has failed.", fkClass, "Merge");
}
delete ntupleDescription->fNtuple;
ntupleDescription->fNtuple = nullptr;
if ( IsVerbose(kVL3) ) {
Message(kVL3, "merge", "pntuple", ntupleDescription->fDescription.fNtupleBooking.name());
Message(kVL3, "merge", "pntuple",
ntupleDescription->GetDescription().GetNtupleBooking().name());
}
}
return true;
// Set new cycle
fNewCycle = true;
return true;
}
//_____________________________________________________________________________
G4bool G4RootPNtupleManager::Reset()
{
// Reset ntuple description, this will delete ntuple if present and
// we have its ownership.
// The ntuples will be recreated with new cycle or new open file.
for ( auto ntupleDescription : fNtupleDescriptionVector ) {
ntupleDescription->Reset();
}
fNtupleVector.clear();
return true;
}
//_____________________________________________________________________________
void G4RootPNtupleManager::Clear()
{
for ( auto ntupleDescription : fNtupleDescriptionVector ) {
delete ntupleDescription->fNtuple;
delete ntupleDescription->GetNtuple();
}
fNtupleDescriptionVector.clear();
@@ -360,7 +432,7 @@ void G4RootPNtupleManager::SetActivation(
G4bool activation)
{
for ( auto ntupleDescription : fNtupleDescriptionVector ) {
ntupleDescription->fDescription.fActivation = activation;
ntupleDescription->GetDescription().SetActivation(activation);
}
}
@@ -370,9 +442,9 @@ void G4RootPNtupleManager::SetActivation(
G4int ntupleId, G4bool activation)
{
auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetActivation");
if ( ! ntupleDescription ) return;
if (ntupleDescription == nullptr) return;
ntupleDescription->fDescription.fActivation = activation;
ntupleDescription->GetDescription().SetActivation(activation);
}
//_____________________________________________________________________________
@@ -380,15 +452,27 @@ G4bool G4RootPNtupleManager::GetActivation(
G4int ntupleId) const
{
auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetActivation");
if ( ! ntupleDescription ) return false;
if (ntupleDescription == nullptr) return false;
return ntupleDescription->fDescription.fActivation;
return ntupleDescription->GetDescription().GetActivation();
}
//_____________________________________________________________________________
void G4RootPNtupleManager::SetNewCycle(G4bool value)
{
fNewCycle = value;
}
//_____________________________________________________________________________
G4bool G4RootPNtupleManager::GetNewCycle() const
{
return fNewCycle;
}
//_____________________________________________________________________________
G4int G4RootPNtupleManager::GetNofNtuples() const
{
return fNtupleVector.size();
return (G4int)fNtupleVector.size();
}
//_____________________________________________________________________________
@@ -397,3 +481,10 @@ void G4RootPNtupleManager::SetNtupleRowWise(G4bool rowWise, G4bool rowMode)
fRowWise = rowWise;
fRowMode = rowMode;
}
//_____________________________________________________________________________
G4bool G4RootPNtupleManager::List(std::ostream& /*output*/, G4bool /*onlyIfActive*/)
{
Warn("Not implemented.", fkClass, "List");
return false;
}
+16 -9
View File
@@ -27,6 +27,7 @@
// Author: Ivana Hrivnacova, 10/09/2014 (ivana@ipno.in2p3.fr)
#include "G4RootRFileManager.hh"
#include "G4RootRFileDef.hh"
#include "G4RootHnRFileManager.hh"
#include "G4AnalysisManagerState.hh"
#include "G4AnalysisUtilities.hh"
@@ -52,8 +53,13 @@ G4RootRFileManager::G4RootRFileManager(const G4AnalysisManagerState& state)
//_____________________________________________________________________________
G4RootRFileManager::~G4RootRFileManager()
{
for ( auto& rfile : fRFiles ) {
delete rfile.second;
// Delete all open file and their directories
for ( auto& mapElement : fRFiles ) {
auto rfileTuple = mapElement.second;
delete std::get<1>(*rfileTuple); // histo directory
delete std::get<2>(*rfileTuple);; // ntuple directory
delete std::get<0>(*rfileTuple); // rfile
delete rfileTuple;
}
}
@@ -80,14 +86,16 @@ G4bool G4RootRFileManager::OpenRFile(const G4String& fileName,
return false;
}
auto newFileTuple = new G4RootRFile(newFile, nullptr, nullptr);
// add file in a map and delete the previous file if it exists
auto it = fRFiles.find(name);
if ( it != fRFiles.end() ) {
delete it->second;
it->second = newFile;
it->second = newFileTuple;
}
else {
fRFiles[name] = newFile;
fRFiles[name] = newFileTuple;
}
Message(kVL1, "open", "read analysis file", name);
@@ -96,16 +104,15 @@ G4bool G4RootRFileManager::OpenRFile(const G4String& fileName,
}
//_____________________________________________________________________________
tools::rroot::file* G4RootRFileManager::GetRFile(const G4String& fileName,
G4bool isPerThread) const
G4RootRFile* G4RootRFileManager::GetRFile(const G4String& fileName,
G4bool isPerThread) const
{
// Get full file name
G4String name = GetFullFileName(fileName, isPerThread);
auto it = fRFiles.find(name);
if ( it != fRFiles.end() )
if (it != fRFiles.end()) {
return it->second;
else {
return nullptr;
}
return nullptr;
}
@@ -62,44 +62,51 @@ G4int G4RootRNtupleManager::ReadNtupleImpl(const G4String& ntupleName,
if ( isUserFileName ) isPerThread = false;
// Get or open a file
auto rfile = fFileManager->GetRFile(fileName, isPerThread);
if ( ! rfile ) {
auto rfileTuple = fFileManager->GetRFile(fileName, isPerThread);
if (rfileTuple == nullptr) {
if ( ! fFileManager->OpenRFile(fileName, isPerThread) ) return kInvalidId;
rfile = fFileManager->GetRFile(fileName, isPerThread);
rfileTuple = fFileManager->GetRFile(fileName, isPerThread);
}
auto rfile = std::get<0>(*rfileTuple);
// Get or open a directory if dirName is defined
tools::rroot::TDirectory* ntupleDirectory = nullptr;
if ( ntupleDirectory == nullptr ) {
// Retrieve directory only once as analysis manager supports only
// 1 ntuple directory par file)
if ( ! dirName.empty() ) {
ntupleDirectory = tools::rroot::find_dir(rfile->dir(), dirName);
if ( ntupleDirectory != nullptr ) {
std::get<2>(*rfileTuple) = ntupleDirectory;
}
else {
G4Analysis::Warn(
"Directory " + dirName + " not found in file " + fileName + ".",
fkClass, "ReadNtupleImpl");
return kInvalidId;
}
}
}
// 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;
}
if ( ntupleDirectory != nullptr ) {
key = ntupleDirectory->find_key(ntupleName);
}
else {
key = rfile->dir().find_key(ntupleName);
}
if ( ! key ) {
if (key == nullptr) {
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 ) {
if (charBuffer == nullptr) {
Warn("Cannot get data buffer for Ntuple " + ntupleName +
" in file " + fileName, fkClass, "ReadNtupleImpl");
delete newDir;
return kInvalidId;
}
@@ -119,7 +126,6 @@ G4int G4RootRNtupleManager::ReadNtupleImpl(const G4String& ntupleName,
delete buffer;
delete tree;
delete newDir;
return kInvalidId;
}
@@ -128,6 +134,7 @@ G4int G4RootRNtupleManager::ReadNtupleImpl(const G4String& ntupleName,
auto id = SetNtuple(rntupleDescription);
Message(kVL2, "read", "ntuple", ntupleName, id > kInvalidId);
return id;