Import Geant4 10.7.0 source tree

This commit is contained in:
Gabriele Cosmo
2020-12-04 12:30:43 +01:00
parent 67ba86d073
commit dab42d2018
3770 changed files with 226369 additions and 286486 deletions
@@ -40,7 +40,7 @@
#include <memory>
class G4CsvFileManager;
class G4CsvNtupleManager;
class G4CsvNtupleFileManager;
class G4CsvAnalysisManager : public G4ToolsAnalysisManager
{
@@ -90,12 +90,11 @@ class G4CsvAnalysisManager : public G4ToolsAnalysisManager
G4bool WriteH3();
G4bool WriteP1();
G4bool WriteP2();
G4bool CloseNtupleFiles();
G4bool Reset();
// data members
G4CsvNtupleManager* fNtupleManager;
std::shared_ptr<G4CsvFileManager> fFileManager;
std::shared_ptr<G4CsvNtupleFileManager> fNtupleFileManager;
};
#include "G4CsvAnalysisManager.icc"
@@ -25,7 +25,9 @@
//
#include "G4CsvNtupleManager.hh"
#include "G4CsvNtupleFileManager.hh"
#include "G4CsvFileManager.hh"
#include "G4AnalysisUtilities.hh"
#include "tools/wcsv_histo"
@@ -64,7 +66,6 @@ G4bool G4CsvAnalysisManager::WriteT(const std::vector<tools::histo::p1d*>& htVec
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()->Message("write", "file", fileName);
#endif
fFileManager->LockHistoDirectoryName();
}
return true;
@@ -105,7 +106,6 @@ G4bool G4CsvAnalysisManager::WriteT(const std::vector<tools::histo::p2d*>& htVec
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()->Message("write", "file", fileName);
#endif
fFileManager->LockHistoDirectoryName();
}
return true;
@@ -145,7 +145,6 @@ G4bool G4CsvAnalysisManager::WriteT(const std::vector<T*>& htVector,
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()->Message("write", "file", fileName);
#endif
fFileManager->LockHistoDirectoryName();
}
return true;
@@ -162,28 +161,28 @@ G4bool G4CsvAnalysisManager::IsOpenFileImpl() const
inline
tools::wcsv::ntuple* G4CsvAnalysisManager::GetNtuple() const
{
return fNtupleManager->GetNtuple();
return fNtupleFileManager->GetNtupleManager()->GetNtuple();
}
//_____________________________________________________________________________
inline
tools::wcsv::ntuple* G4CsvAnalysisManager::GetNtuple(G4int ntupleId) const
{
return fNtupleManager->GetNtuple(ntupleId);
return fNtupleFileManager->GetNtupleManager()->GetNtuple(ntupleId);
}
//_____________________________________________________________________________
inline
std::vector<tools::wcsv::ntuple*>::iterator G4CsvAnalysisManager::BeginNtuple()
{
return fNtupleManager->BeginNtuple();
return fNtupleFileManager->GetNtupleManager()->BeginNtuple();
}
//_____________________________________________________________________________
inline
std::vector<tools::wcsv::ntuple*>::iterator G4CsvAnalysisManager::EndNtuple()
{
return fNtupleManager->EndNtuple();
return fNtupleFileManager->GetNtupleManager()->EndNtuple();
}
//_____________________________________________________________________________
@@ -191,7 +190,7 @@ inline
std::vector<tools::wcsv::ntuple*>::const_iterator
G4CsvAnalysisManager::BeginConstNtuple() const
{
return fNtupleManager->BeginConstNtuple();
return fNtupleFileManager->GetNtupleManager()->BeginConstNtuple();
}
//_____________________________________________________________________________
@@ -199,19 +198,19 @@ inline
std::vector<tools::wcsv::ntuple*>::const_iterator
G4CsvAnalysisManager::EndConstNtuple() const
{
return fNtupleManager->EndConstNtuple();
return fNtupleFileManager->GetNtupleManager()->EndConstNtuple();
}
//_____________________________________________________________________________
inline
void G4CsvAnalysisManager::SetIsCommentedHeader(G4bool isCommentedHeader)
{
fNtupleManager->SetIsCommentedHeader(isCommentedHeader);
fNtupleFileManager->GetNtupleManager()->SetIsCommentedHeader(isCommentedHeader);
}
//_____________________________________________________________________________
inline
void G4CsvAnalysisManager::SetIsHippoHeader(G4bool isHippoHeader)
{
fNtupleManager->SetIsHippoHeader(isHippoHeader);
fNtupleFileManager->GetNtupleManager()->SetIsHippoHeader(isHippoHeader);
}
+24 -12
View File
@@ -31,31 +31,43 @@
#ifndef G4CsvFileManager_h
#define G4CsvFileManager_h 1
#include "G4VFileManager.hh"
#include "G4VTFileManager.hh"
#include "G4TNtupleDescription.hh"
#include "globals.hh"
#include "tools/wcsv_ntuple"
class G4CsvFileManager : public G4VFileManager
// Type aliases
using CsvNtupleDescription = G4TNtupleDescription<tools::wcsv::ntuple, std::ofstream>;
class G4CsvFileManager : public G4VTFileManager<std::ofstream>
{
public:
explicit G4CsvFileManager(const G4AnalysisManagerState& state);
~G4CsvFileManager();
// Type aliases
using NtupleType = tools::wcsv::ntuple;
using NtupleDescriptionType = G4TNtupleDescription<NtupleType>;
// Methods to manipulate files
using G4BaseFileManager::GetNtupleFileName;
using G4VTFileManager<std::ofstream>::WriteFile;
using G4VTFileManager<std::ofstream>::CloseFile;
// Methods to manipulate output files
virtual G4bool OpenFile(const G4String& fileName) final;
virtual G4bool WriteFile() final;
virtual G4bool CloseFile() final;
virtual G4String GetFileType() const final { return "csv"; }
// Specific methods for files per objects
G4bool CreateNtupleFile(NtupleDescriptionType* ntupleDescription);
G4bool CloseNtupleFile(NtupleDescriptionType* ntupleDescription);
G4bool CreateNtupleFile(CsvNtupleDescription* ntupleDescription);
G4bool CloseNtupleFile(CsvNtupleDescription* ntupleDescription);
protected:
// Methods derived from templated base class
virtual std::shared_ptr<std::ofstream> CreateFileImpl(const G4String& fileName) final;
virtual G4bool WriteFileImpl(std::shared_ptr<std::ofstream> file) final;
virtual G4bool CloseFileImpl(std::shared_ptr<std::ofstream> file) final;
private:
// Utility method
G4String GetNtupleFileName(CsvNtupleDescription* ntupleDescription);
};
#endif
@@ -0,0 +1,63 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// The manager for histogram/profile Csv file output.
// Author: Ivana Hrivnacova, 15/09/2020 (ivana@ipno.in2p3.fr)
#ifndef G4CsvHnFileManager_h
#define G4CsvHnFileManager_h 1
#include "G4VTHnFileManager.hh"
class G4CsvFileManager;
template <typename HT>
class G4CsvHnFileManager : public G4VTHnFileManager<HT>
{
public:
G4CsvHnFileManager(G4CsvFileManager* fileManger)
: G4VTHnFileManager<HT>(), fFileManager(fileManger) {}
virtual ~G4CsvHnFileManager() {}
// Methods for writing objects
virtual 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;
private:
// Methods
G4bool Write(std::ofstream& hnfile, HT* ht);
// Data members
G4CsvFileManager* fFileManager;
};
#include "G4CsvHnFileManager.icc"
#endif
@@ -0,0 +1,125 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// Author: Ivana Hrivnacova, 15/09/2020 (ivana@ipno.in2p3.fr)
#include "G4AnalysisUtilities.hh"
#include "tools/wcsv_histo"
#include "tools/histo/h1d"
#include "tools/histo/h2d"
#include "tools/histo/h3d"
#include "tools/histo/p1d"
#include "tools/histo/p2d"
#include <iostream>
//_____________________________________________________________________________
template <>
inline
G4bool G4CsvHnFileManager<tools::histo::p1d>::Write(
std::ofstream& hnfile, tools::histo::p1d* ht)
{
return tools::wcsv::pto(hnfile, ht->s_cls(), *ht);
}
//_____________________________________________________________________________
template <>
inline
G4bool G4CsvHnFileManager<tools::histo::p2d>::Write(
std::ofstream& hnfile, tools::histo::p2d* ht)
{
return tools::wcsv::pto(hnfile, ht->s_cls(), *ht);
}
//_____________________________________________________________________________
template <typename HT>
inline
G4bool G4CsvHnFileManager<HT>::Write(
std::ofstream& hnfile, HT* ht)
{
return tools::wcsv::hto(hnfile, ht->s_cls(), *ht);
}
//_____________________________________________________________________________
template <typename HT>
inline
G4bool G4CsvHnFileManager<HT>::WriteExtra(
HT* ht, const G4String& htName, const G4String& fileName)
{
// create a new file
std::ofstream hnFile(fileName);
// Do nothing if there is no file
if ( ! hnFile.is_open() ) return false;
auto result = Write(hnFile, ht);
if ( ! result ) {
G4ExceptionDescription description;
description << " " << "saving " << G4Analysis::GetHnType<HT>() << " " << htName << " failed";
G4Exception("G4CsvAnalysisManager::WriteExtra()",
"Analysis_W022", JustWarning, description);
return false;
}
hnFile.close();
return true;
}
//_____________________________________________________________________________
template <typename HT>
inline
G4bool G4CsvHnFileManager<HT>::Write(
HT* ht, const G4String& htName, G4String& fileName)
{
if ( fileName.empty() ) {
// should not happen
G4cerr << "!!! Csv file name not defined." << G4endl;
G4cerr << "!!! Write " << htName << " failed." << G4endl;
return false;
}
auto hnFile = fFileManager->GetTFile(fileName, false);
if ( ! hnFile ) {
// If histogram file name was not defined per object
// a file should be created from the provided default file name
auto hnFileName = fFileManager->GetHnFileName(G4Analysis::GetHnType<HT>(), htName);
if ( ! hnFileName.empty() ) {
hnFile = fFileManager->CreateTFile(hnFileName);
}
if ( ! hnFile ) {
G4ExceptionDescription description;
description << "Failed to get Csv file " << fileName;
G4Exception("G4CsvHnFileManager<HT>::Write()",
"Analysis_W022", JustWarning, description);
return false;
} else {
fileName = hnFileName;
}
}
return Write(*hnFile, ht);
}
@@ -0,0 +1,81 @@
//
// ********************************************************************
// * 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. *
// ********************************************************************
//
// Manager class for ntuple Csv file output.
//
// Author: Ivana Hrivnacova, 15/09/2020 (ivana@ipno.in2p3.fr)
#ifndef G4CsvNtupleFileManager_h
#define G4CsvNtupleFileManager_h 1
#include "G4VNtupleFileManager.hh"
#include "globals.hh"
class G4CsvFileManager;
class G4CsvNtupleManager;
class G4VNtupleManager;
class G4NtupleBookingManager;
class G4CsvNtupleFileManager : public G4VNtupleFileManager
{
friend class G4CsvAnalysisManager;
public:
explicit G4CsvNtupleFileManager(const G4AnalysisManagerState& state);
~G4CsvNtupleFileManager();
virtual std::shared_ptr<G4VNtupleManager> CreateNtupleManager() override;
// Methods to be performed at file management
virtual G4bool ActionAtOpenFile(const G4String& fileName) override;
virtual G4bool ActionAtWrite() override;
virtual G4bool ActionAtCloseFile(G4bool reset) override;
virtual G4bool Reset() override;
void SetFileManager(std::shared_ptr<G4CsvFileManager> fileManager);
std::shared_ptr<G4CsvNtupleManager> GetNtupleManager() const;
private:
// methods
G4bool CloseNtupleFiles();
// data members
std::shared_ptr<G4CsvFileManager> fFileManager;
std::shared_ptr<G4CsvNtupleManager> fNtupleManager;
};
// inline functions
inline void G4CsvNtupleFileManager::SetFileManager(
std::shared_ptr<G4CsvFileManager> fileManager)
{ fFileManager = fileManager; }
inline std::shared_ptr<G4CsvNtupleManager> G4CsvNtupleFileManager::GetNtupleManager() const
{ return fNtupleManager; }
#endif
@@ -38,45 +38,43 @@
#include <memory>
// Types alias
using CsvNtupleDescription = G4TNtupleDescription<tools::wcsv::ntuple, std::ofstream>;
class G4CsvFileManager;
class G4CsvNtupleManager : public G4TNtupleManager<tools::wcsv::ntuple>
class G4CsvNtupleManager : public G4TNtupleManager<tools::wcsv::ntuple,
std::ofstream>
{
friend class G4CsvAnalysisManager;
friend class G4CsvNtupleFileManager;
public:
explicit G4CsvNtupleManager(const G4AnalysisManagerState& state);
~G4CsvNtupleManager();
private:
// Types alias
using NtupleType = tools::wcsv::ntuple;
using NtupleDescriptionType = G4TNtupleDescription<NtupleType>;
// Functions specific to the output type
// Set methods
void SetFileManager(std::shared_ptr<G4CsvFileManager> fileManager);
// Access to ntuple vector (needed for Write())
const std::vector<NtupleDescriptionType*>& GetNtupleDescriptionVector() const;
const std::vector<CsvNtupleDescription*>& GetNtupleDescriptionVector() const;
void SetIsCommentedHeader(G4bool isCommentedHeader);
void SetIsHippoHeader(G4bool isHippoHeader);
// Methods from the templated base class
//
virtual void CreateTNtuple(
NtupleDescriptionType* ntupleDescription,
const G4String& name, const G4String& title) final;
virtual void CreateTNtupleFromBooking(
NtupleDescriptionType* ntupleDescription) final;
CsvNtupleDescription* ntupleDescription) final;
virtual void FinishTNtuple(
NtupleDescriptionType* ntupleDescription,
CsvNtupleDescription* ntupleDescription,
G4bool fromBooking) final;
G4bool WriteHeader(NtupleType* ntuple) const;
G4bool WriteHeader(tools::wcsv::ntuple* ntuple) const;
// data members
std::shared_ptr<G4CsvFileManager> fFileManager;
@@ -90,7 +88,7 @@ inline void
G4CsvNtupleManager::SetFileManager(std::shared_ptr<G4CsvFileManager> fileManager)
{ fFileManager = fileManager; }
inline const std::vector<G4TNtupleDescription<tools::wcsv::ntuple>*>&
inline const std::vector<G4TNtupleDescription<tools::wcsv::ntuple, std::ofstream>*>&
G4CsvNtupleManager::GetNtupleDescriptionVector() const
{ return fNtupleDescriptionVector; }
@@ -101,4 +99,3 @@ inline void G4CsvNtupleManager::SetIsHippoHeader(G4bool isHippoHeader)
{ fIsHippoHeader = isHippoHeader; }
#endif
@@ -45,6 +45,8 @@ class G4CsvRFileManager : public G4BaseFileManager
explicit G4CsvRFileManager(const G4AnalysisManagerState& state);
~G4CsvRFileManager();
virtual G4String GetFileType() const final { return "csv"; }
// Methods to manipulate input files
virtual G4bool OpenRFile(const G4String& fileName);
+4
View File
@@ -13,6 +13,9 @@ geant4_define_module(NAME G4csv
G4CsvAnalysisReader.hh
G4CsvAnalysisReader.icc
G4CsvFileManager.hh
G4CsvHnFileManager.hh
G4CsvHnFileManager.icc
G4CsvNtupleFileManager.hh
G4CsvNtupleManager.hh
G4CsvRFileManager.hh
G4CsvRNtupleManager.hh
@@ -22,6 +25,7 @@ geant4_define_module(NAME G4csv
G4CsvAnalysisManager.cc
G4CsvAnalysisReader.cc
G4CsvFileManager.cc
G4CsvNtupleFileManager.cc
G4CsvNtupleManager.cc
G4CsvRFileManager.cc
G4CsvRNtupleManager.cc
+38 -64
View File
@@ -28,7 +28,7 @@
#include "G4CsvAnalysisManager.hh"
#include "G4CsvFileManager.hh"
#include "G4CsvNtupleManager.hh"
#include "G4CsvNtupleFileManager.hh"
#include "G4AnalysisVerbose.hh"
#include "G4AnalysisManagerState.hh"
#include "G4UnitsTable.hh"
@@ -75,8 +75,8 @@ G4bool G4CsvAnalysisManager::IsInstance()
//_____________________________________________________________________________
G4CsvAnalysisManager::G4CsvAnalysisManager(G4bool isMaster)
: G4ToolsAnalysisManager("Csv", isMaster),
fNtupleManager(nullptr),
fFileManager(nullptr)
fFileManager(nullptr),
fNtupleFileManager(nullptr)
{
if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
G4ExceptionDescription description;
@@ -89,14 +89,15 @@ G4CsvAnalysisManager::G4CsvAnalysisManager(G4bool isMaster)
if ( isMaster ) fgMasterInstance = this;
fgInstance = this;
fNtupleManager = new G4CsvNtupleManager(fState);
// File Manager
fFileManager = std::make_shared<G4CsvFileManager>(fState);
fNtupleManager->SetFileManager(fFileManager);
// The managers will be deleted by the base class
// Set managers to base class which takes then their ownreship
SetNtupleManager(fNtupleManager);
SetFileManager(fFileManager);
// Ntuple file manager
fNtupleFileManager = std::make_shared<G4CsvNtupleFileManager>(fState);
fNtupleFileManager->SetFileManager(fFileManager);
fNtupleFileManager->SetBookingManager(fNtupleBookingManager);
}
//_____________________________________________________________________________
@@ -110,30 +111,6 @@ G4CsvAnalysisManager::~G4CsvAnalysisManager()
// private methods
//
//_____________________________________________________________________________
G4bool G4CsvAnalysisManager::CloseNtupleFiles()
{
auto ntupleVector
= fNtupleManager->GetNtupleDescriptionVector();
// Close ntuple files
for ( auto ntupleDescription : ntupleVector) {
fFileManager->CloseNtupleFile(ntupleDescription);
}
return true;
}
//
// protected methods
//
//
// private methods
//
//_____________________________________________________________________________
G4bool G4CsvAnalysisManager::WriteH1()
{
@@ -264,7 +241,7 @@ G4bool G4CsvAnalysisManager::Reset()
auto result = G4ToolsAnalysisManager::Reset();
finalResult = finalResult && result;
result = fNtupleManager->Reset(true);
result = fNtupleFileManager->Reset();
finalResult = finalResult && result;
return finalResult;
@@ -273,24 +250,23 @@ G4bool G4CsvAnalysisManager::Reset()
//_____________________________________________________________________________
G4bool G4CsvAnalysisManager::OpenFileImpl(const G4String& fileName)
{
// Create ntuple manager(s)
// and set it to base class which takes then their ownership
SetNtupleManager(fNtupleFileManager->CreateNtupleManager());
auto finalResult = true;
auto result = fFileManager->SetFileName(fileName);
// Save file name in file manager
auto result = fFileManager->OpenFile(fileName);
finalResult = finalResult && result;
// Only lock file name in file manager
result = fFileManager->OpenFile(fileName);
// Open ntuple files and create ntuples from bookings
result = fNtupleFileManager->ActionAtOpenFile(fFileManager->GetFullFileName());
finalResult = finalResult && result;
// Histogram and profile files are created/closed indivudually for each
// object in WriteHn{Pn] function
// Create ntuples if they are booked
// (The files will be created with creating ntuples)
fNtupleManager->CreateNtuplesFromBooking();
return finalResult;
}
}
//_____________________________________________________________________________
G4bool G4CsvAnalysisManager::WriteImpl()
{
@@ -298,8 +274,9 @@ G4bool G4CsvAnalysisManager::WriteImpl()
auto finalResult = true;
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("write", "files", "");
}
#endif
@@ -347,9 +324,9 @@ G4bool G4CsvAnalysisManager::WriteImpl()
//}
#ifdef G4VERBOSE
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()
->Message("write", "files", "", finalResult);
if ( fState.GetVerboseL2() ) {
fState.GetVerboseL2()->Message("write", "files", "", finalResult);
}
#endif
return result;
@@ -360,29 +337,26 @@ G4bool G4CsvAnalysisManager::CloseFileImpl(G4bool reset)
{
auto finalResult = true;
// Unlock file name only
auto result = fFileManager->CloseFile();
// close open files
auto result = fFileManager->CloseFiles();
finalResult = finalResult && result;
// Histogram and profile files are created/closed indivudually for each
// object in WriteHn{Pn] function
// In sequential mode or in MT mode only on workers
result = CloseNtupleFiles();
result = fNtupleFileManager->ActionAtCloseFile(reset);
finalResult = finalResult && result;
// reset data
// Reset data
if ( reset ) {
result = Reset();
} else {
// ntuple must be reset
result = fNtupleManager->Reset(true);
if ( ! result ) {
G4ExceptionDescription description;
description << " " << "Resetting data failed";
G4Exception("G4CsvAnalysisManager::CloseFile()",
"Analysis_W021", JustWarning, description);
}
}
if ( ! result ) {
G4ExceptionDescription description;
description << " " << "Resetting data failed";
G4Exception("G4CsvAnalysisManager::CloseFile()",
"Analysis_W021", JustWarning, description);
}
finalResult = finalResult && result;
return finalResult;
@@ -67,7 +67,7 @@ void* ReadObject(std::istream& hnFile,
<< "Cannot get "<< objectType << " in file " << fileName;
G4String inFunctionFull = "G4CsvAnalysisReader::";
inFunctionFull.append(inFunction);
G4Exception(inFunctionFull, "Analysis_WRnullptr11", JustWarning, description);
G4Exception(inFunctionFull, "Analysis_WR011", JustWarning, description);
return nullptr;
}
if ( objectTypeInFile != objectType ) {
+129 -60
View File
@@ -27,17 +27,86 @@
// Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
#include "G4CsvFileManager.hh"
#include "G4CsvHnFileManager.hh"
#include "G4AnalysisManagerState.hh"
#include "G4AnalysisUtilities.hh"
using namespace G4Analysis;
using namespace tools;
//_____________________________________________________________________________
G4CsvFileManager::G4CsvFileManager(const G4AnalysisManagerState& state)
: G4VFileManager(state)
{}
: G4VTFileManager(state)
{
// Create helpers defined in the base class
fH1FileManager = std::make_shared<G4CsvHnFileManager<histo::h1d>>(this);
fH2FileManager = std::make_shared<G4CsvHnFileManager<histo::h2d>>(this);
fH3FileManager = std::make_shared<G4CsvHnFileManager<histo::h3d>>(this);
fP1FileManager = std::make_shared<G4CsvHnFileManager<histo::p1d>>(this);
fP2FileManager = std::make_shared<G4CsvHnFileManager<histo::p2d>>(this);
}
//_____________________________________________________________________________
G4CsvFileManager::~G4CsvFileManager()
{}
//
// private methods
//
//_____________________________________________________________________________
G4String G4CsvFileManager::GetNtupleFileName(CsvNtupleDescription* ntupleDescription)
{
// get ntuple file name
auto ntupleFileName = ntupleDescription->fFileName;
if ( ntupleFileName.size() ) {
// update filename per object per thread
ntupleFileName = GetTnFileName(ntupleFileName, GetFileType());
} else {
// compose ntuple file name from the default file name
ntupleFileName = GetNtupleFileName(ntupleDescription->fNtupleBooking.name());
}
return ntupleFileName;
}
//
// protected methods
//
//_____________________________________________________________________________
std::shared_ptr<std::ofstream> G4CsvFileManager::CreateFileImpl(const G4String& fileName)
{
std::shared_ptr<std::ofstream> file = std::make_shared<std::ofstream>(fileName);
if ( file->fail() ) {
file = nullptr;
G4ExceptionDescription description;
description << " " << "Cannot create file " << fileName;
G4Exception("G4CsvFileManager::CreateFileImpl()",
"Analysis_W001", JustWarning, description);
return nullptr;
}
return file;
}
//_____________________________________________________________________________
G4bool G4CsvFileManager::WriteFileImpl(std::shared_ptr<std::ofstream> /*file*/)
{
// Nothing to be done here
return true;
}
//_____________________________________________________________________________
G4bool G4CsvFileManager::CloseFileImpl(std::shared_ptr<std::ofstream> file)
{
if ( ! file ) return false;
// close file
file->close();
return true;
}
//
// public methods
//
@@ -48,85 +117,85 @@ G4bool G4CsvFileManager::OpenFile(const G4String& fileName)
// Keep file name
fFileName = fileName;
fLockFileName = true;
fLockNtupleDirectoryName = true;
fIsOpenFile = true;
return true;
}
//_____________________________________________________________________________
G4bool G4CsvFileManager::WriteFile()
G4bool G4CsvFileManager::CreateNtupleFile(
CsvNtupleDescription* ntupleDescription)
{
// nothing to be done for Csv file
return true;
// get ntuple file name per object (if defined)
auto ntupleFileName = GetNtupleFileName(ntupleDescription);
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4()->Message("create", "ntuple file", ntupleFileName);
}
#endif
// update file name if it is already in use
while ( GetTFile(ntupleFileName, false) ) {
// the file is already in use
auto oldName = ntupleDescription->fFileName;
auto newName = GetBaseName(oldName) + "_bis." + GetExtension(oldName);
ntupleDescription->fFileName = newName;
G4ExceptionDescription description;
description
<< "Ntuple filename " << oldName << " is already in use." << G4endl
<< "It will be replaced with : " << newName;
G4Exception("G4CsvFileManager::CreateFileImpl()",
"Analysis_W001", JustWarning, description);
ntupleFileName = GetNtupleFileName(ntupleDescription);
}
ntupleDescription->fFile = CreateTFile(ntupleFileName);
#ifdef G4VERBOSE
if ( fState.GetVerboseL2() ) {
fState.GetVerboseL2()->Message("create", "ntuple file", ntupleFileName);
}
#endif
return (ntupleDescription->fFile != nullptr);
}
//_____________________________________________________________________________
G4bool G4CsvFileManager::CloseFile()
{
fLockFileName = false;
fIsOpenFile = false;
return true;
}
//_____________________________________________________________________________
G4bool G4CsvFileManager::CreateNtupleFile(
G4TNtupleDescription<tools::wcsv::ntuple>* ntupleDescription)
{
G4String ntupleName = ntupleDescription->fNtupleBooking.name();
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()
->Message("create", "file", GetNtupleFileName(ntupleName));
#endif
auto ntupleFile = new std::ofstream(GetNtupleFileName(ntupleName));
if ( ntupleFile->fail() ) {
delete ntupleFile;
G4ExceptionDescription description;
description << " " << "Cannot open file "
<< GetNtupleFileName(ntupleName);
G4Exception("G4CsvFileManager::CreateNtupleFile()",
"Analysis_W001", JustWarning, description);
return false;
}
#ifdef G4VERBOSE
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()
->Message("create", "file", GetNtupleFileName(ntupleName));
#endif
ntupleDescription->fFile = ntupleFile;
return true;
}
//_____________________________________________________________________________
G4bool G4CsvFileManager::CloseNtupleFile(
G4TNtupleDescription<tools::wcsv::ntuple>* ntupleDescription)
CsvNtupleDescription* ntupleDescription)
{
// Do nothing if there is no file
if ( ! ntupleDescription->fFile ) return true;
G4String ntupleName = ntupleDescription->fNtupleBooking.name();
auto finalResult = true;
auto ntupleFileName = GetNtupleFileName(ntupleDescription);
#ifdef G4VERBOSE
if ( fState.GetVerboseL4() )
fState.GetVerboseL4()
->Message("close", "file", GetNtupleFileName(ntupleName));
if ( fState.GetVerboseL4() ) {
fState.GetVerboseL4() ->Message("close", "ntuple file", ntupleFileName);
}
#endif
// close file
ntupleDescription->fFile->close();
auto result = CloseTFile(ntupleFileName);
finalResult = result && finalResult;
// Notify not empty file
result = SetIsEmpty(ntupleFileName, ! ntupleDescription->fHasFill);
finalResult = result && finalResult;
// Reset file info in ntuple description
ntupleDescription->fFile.reset();
#ifdef G4VERBOSE
if ( fState.GetVerboseL1() )
fState.GetVerboseL1()
->Message("close", "file", GetNtupleFileName(ntupleName));
if ( fState.GetVerboseL2() ) {
fState.GetVerboseL2()->Message("close", "ntuple file", ntupleFileName);
}
#endif
return true;
}
return finalResult;
}
@@ -0,0 +1,133 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// Author: Ivana Hrivnacova, 15/09/2020 (ivana@ipno.in2p3.fr)
#include "G4CsvNtupleFileManager.hh"
#include "G4CsvNtupleManager.hh"
#include "G4CsvFileManager.hh"
#include "G4AnalysisManagerState.hh"
#include "G4AnalysisUtilities.hh"
#include "G4Threading.hh"
#include <iostream>
using namespace G4Analysis;
//_____________________________________________________________________________
G4CsvNtupleFileManager::G4CsvNtupleFileManager(const G4AnalysisManagerState& state)
: G4VNtupleFileManager(state, "csv"),
fFileManager(nullptr),
fNtupleManager(nullptr)
{}
//_____________________________________________________________________________
G4CsvNtupleFileManager::~G4CsvNtupleFileManager()
{}
//
// private methods
//
//_____________________________________________________________________________
G4bool G4CsvNtupleFileManager::CloseNtupleFiles()
{
// Close ntuple files
auto finalResult = true;
auto ntupleVector = fNtupleManager->GetNtupleDescriptionVector();
for ( auto ntupleDescription : ntupleVector) {
auto result = fFileManager->CloseNtupleFile(ntupleDescription);
finalResult = finalResult && result;
}
return finalResult;
}
//
// public methods
//
//_____________________________________________________________________________
std::shared_ptr<G4VNtupleManager> G4CsvNtupleFileManager::CreateNtupleManager()
{
// G4cout << "G4CsvNtupleFileManager::CreateNtupleManager" << G4endl;
fNtupleManager = std::make_shared<G4CsvNtupleManager>(fState);
fNtupleManager->SetFileManager(fFileManager);
return fNtupleManager;
}
//_____________________________________________________________________________
G4bool G4CsvNtupleFileManager::ActionAtOpenFile(const G4String& /*fileName*/)
{
// G4cout << "G4CsvNtupleFileManager::ActionAtOpenFile" << G4endl;
// Create ntuples if they are booked
// (The files will be created with creating ntuples)
fNtupleManager->CreateNtuplesFromBooking(
fBookingManager->GetNtupleBookingVector());
return true;
}
//_____________________________________________________________________________
G4bool G4CsvNtupleFileManager::ActionAtWrite()
{
return true;
}
//_____________________________________________________________________________
G4bool G4CsvNtupleFileManager::ActionAtCloseFile(G4bool reset)
{
auto finalResult = true;
// close ntuple files
auto result = CloseNtupleFiles();
finalResult = finalResult && result;
if ( ! reset ) {
// The ntuples must be always reset when closing file)
result = Reset();
if ( ! result ) {
G4ExceptionDescription description;
description << " " << "Resetting data failed";
G4Exception("G4CsvNtupleFileManager::CloseFile()",
"Analysis_W021", JustWarning, description);
}
finalResult = finalResult && result;
}
return finalResult;
}
//_____________________________________________________________________________
G4bool G4CsvNtupleFileManager::Reset()
{
return fNtupleManager->Reset(true);
}
+21 -30
View File
@@ -43,7 +43,7 @@ using namespace G4Analysis;
//_____________________________________________________________________________
G4CsvNtupleManager::G4CsvNtupleManager(const G4AnalysisManagerState& state)
: G4TNtupleManager<tools::wcsv::ntuple>(state),
: G4TNtupleManager<tools::wcsv::ntuple, std::ofstream>(state),
fFileManager(nullptr),
fIsCommentedHeader(true),
fIsHippoHeader(false)
@@ -57,41 +57,23 @@ G4CsvNtupleManager::~G4CsvNtupleManager()
// private methods
//
//_____________________________________________________________________________
void G4CsvNtupleManager::CreateTNtuple(
G4TNtupleDescription<tools::wcsv::ntuple>* ntupleDescription,
const G4String& /*name*/, const G4String& title)
{
// Create ntuple if the file is open (what means here that
// a filename was already set)
if ( fFileManager->GetFileName().size() ) {
if ( fFileManager->CreateNtupleFile(ntupleDescription) ) {
ntupleDescription->fNtuple
= new tools::wcsv::ntuple(*(ntupleDescription->fFile));
// ntuple object is deleted when closing a file
(ntupleDescription->fNtuple)->set_title(title);
fNtupleVector.push_back(ntupleDescription->fNtuple);
}
}
}
//_____________________________________________________________________________
void G4CsvNtupleManager::CreateTNtupleFromBooking(
G4TNtupleDescription<tools::wcsv::ntuple>* ntupleDescription)
CsvNtupleDescription* ntupleDescription)
{
// create a file for this ntuple
if ( ! fFileManager->CreateNtupleFile(ntupleDescription) ) return;
// create a file for this ntuple
if ( ! fFileManager->CreateNtupleFile(ntupleDescription) ) return;
// create ntuple
ntupleDescription->fNtuple
= new tools::wcsv::ntuple(
*(ntupleDescription->fFile), G4cerr, ntupleDescription->fNtupleBooking);
fNtupleVector.push_back(ntupleDescription->fNtuple);
// create ntuple
ntupleDescription->fNtuple
= new tools::wcsv::ntuple(
*(ntupleDescription->fFile), G4cerr, ntupleDescription->fNtupleBooking);
fNtupleVector.push_back(ntupleDescription->fNtuple);
}
//_____________________________________________________________________________
void G4CsvNtupleManager::FinishTNtuple(
G4TNtupleDescription<tools::wcsv::ntuple>* ntupleDescription,
CsvNtupleDescription* ntupleDescription,
G4bool /*fromBooking*/)
{
@@ -103,11 +85,20 @@ void G4CsvNtupleManager::FinishTNtuple(
CreateTNtupleFromBooking(ntupleDescription);
}
// Return if creating ntuple failed
if ( ! ntupleDescription->fNtuple ) {
G4ExceptionDescription description;
description << "Creating ntuple has failed. ";
G4Exception("G4CsvNtupleManager::FinishTNtuple()",
"Analysis_W022", JustWarning, description);
return;
}
// Write header if ntuple already exists
if ( ! WriteHeader(ntupleDescription->fNtuple) ) {
G4ExceptionDescription description;
description << " "
<< "Writing ntuple header has failed. ";
description << "Writing ntuple header has failed. ";
G4Exception("G4CsvNtupleManager::FinishTNtuple()",
"Analysis_W022", JustWarning, description);
}