Import Geant4 11.3.0 source tree

This commit is contained in:
Gabriele Cosmo
2024-12-06 11:11:40 +01:00
parent e58e650b32
commit 32390e802b
1984 changed files with 98713 additions and 83996 deletions
@@ -0,0 +1,96 @@
//
// ********************************************************************
// * 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. *
// ********************************************************************
//
// Class template for accumulable arrays handled by Geant4 analysis
//
// Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 18/07/2024
#ifndef G4AccArray_h
#define G4AccArray_h 1
#include "G4VAccumulable.hh"
#include "G4MergeMode.hh"
#include "globals.hh"
#include <array>
template <class T, std::size_t N>
class G4AccArray : public G4VAccumulable
{
public:
// Default constructor (1)
// Constructs the container with the default init value
G4AccArray(const G4String& name = "",
const T& value = 0,
G4MergeMode mergeMode = G4MergeMode::kAddition);
// Constructor (2)
// Constructs the container with the provided initializer list
// and defaults for all other members
template <typename... Args>
G4AccArray(Args&&... args);
// // Constructor (3)
// // Constructs the container with the provided name and initializer list
// // and defaults for the other members
template <typename First, typename... Args>
G4AccArray(const First& firstArg, Args&&... args);
// Copy constructor
G4AccArray(const G4AccArray& rhs) = default;
// Move constructor
G4AccArray(G4AccArray&& rhs) = default;
// Destructor
~G4AccArray() override = default;
// operators
inline T& operator[](typename std::array<T,N>::size_type i) { return fArray[i]; }
// Methods
void Merge(const G4VAccumulable& other) final;
void Reset() final;
void Print(G4PrintOptions options = G4PrintOptions()) const final;
void SetMergeMode(G4MergeMode value) final;
// Get methods
G4AccType GetType() const final { return G4AccType::kArray; }
std::array<T,N>& GetArray();
const std::array<T,N>& GetArray() const;
private:
// Data members
std::array<T,N> fArray {};
T fInitValue = 0;
G4MergeFunction<T> fMergeFunction;
};
// inline functions
#include "G4AccArray.icc"
#endif
@@ -0,0 +1,154 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 18/07/2024
#include <algorithm>
//
// public functions
//
//_____________________________________________________________________________
template <class T, std::size_t N>
G4AccArray<T, N>::G4AccArray(
const G4String& name, const T& value, G4MergeMode mergeMode)
: G4VAccumulable(name, mergeMode),
fInitValue(value),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccArray ctor1" << G4endl;
}
for (auto& element : fArray ) {
element = fInitValue;
}
}
//_____________________________________________________________________________
template <class T, std::size_t N>
template <typename... Args>
G4AccArray<T, N>::G4AccArray(Args&&... args)
: G4VAccumulable(),
fArray{{std::forward<Args>(args)...}}, fMergeFunction(GetMergeFunction<T>(fMergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccArray ctor2" << G4endl;
}
}
//_____________________________________________________________________________
template <class T, std::size_t N>
template <typename First, typename... Args>
G4AccArray<T, N>::G4AccArray(const First& firstArg, Args&&... args)
: G4VAccumulable(firstArg),
fArray{{std::forward<Args>(args)...}},
fMergeFunction(GetMergeFunction<T>(fMergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccArray ctor3" << G4endl;
}
}
//_____________________________________________________________________________
template <class T, std::size_t N>
void G4AccArray<T, N>::Merge(const G4VAccumulable& other)
{
const auto& otherArrayAcc = static_cast<const G4AccArray<T,N>&>(other);
const auto& otherArray = otherArrayAcc.GetArray();
if (G4Accumulables::VerboseLevel > 2 ) {
G4cout << "G4AccArray<T, N>::Merge: " << G4endl;
G4cout << "destination: ";
for (auto v : this->fArray) {
G4cout << v << ", ";
}
G4cout << G4endl;
G4cout << "merged data: ";
for (auto v : otherArray) {
G4cout << v << ", ";
}
G4cout << G4endl;
}
std::transform(fArray.begin(), fArray.end(), otherArray.begin(),
fArray.begin(), fMergeFunction);
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccArray<T, N>::Merge: done" << G4endl;
}
}
//_____________________________________________________________________________
template <class T, std::size_t N>
void G4AccArray<T, N>::Reset()
{
for (auto& value : fArray) {
value = fInitValue;
}
}
//_____________________________________________________________________________
template <class T, std::size_t N>
void G4AccArray<T, N>::Print(G4PrintOptions options) const
{
if (options.Has(G4PrintOptions::kType)) {
G4cout << "array<" << typeid(fInitValue).name() << ">: ";
}
PrintBase(options);
bool first = true;
for (auto& value : fArray) {
if (! first) { G4cout << ", "; }
G4cout << value;
first = false;
}
G4cout << G4endl;
}
//_____________________________________________________________________________
template <class T, std::size_t N>
void G4AccArray<T, N>::SetMergeMode(G4MergeMode value)
{
G4VAccumulable::SetMergeMode(value);
fMergeFunction = GetMergeFunction<T>(value);
}
//_____________________________________________________________________________
template <class T, std::size_t N>
std::array<T,N>& G4AccArray<T, N>::GetArray()
{
return fArray;
}
//_____________________________________________________________________________
template <class T, std::size_t N>
const std::array<T,N>& G4AccArray<T, N>::GetArray() const
{
return fArray;
}
@@ -0,0 +1,200 @@
//
// ********************************************************************
// * 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. *
// ********************************************************************
//
// Class template for accumulable maps handled by Geant4 analysis
//
// Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 19/07/2024
#ifndef G4AccMap_h
#define G4AccMap_h 1
#include "G4VAccumulable.hh"
#include "G4MergeMode.hh"
#include "globals.hh"
#include <map>
template <class Key,
class T,
class Compare = std::less<Key>,
class Allocator = std::allocator<std::pair<const Key, T>>>
class G4AccMap : public G4VAccumulable
{
public:
// ctors to be supported
// (https://en.cppreference.com/w/cpp/container/map/map)
//
// Default constructor (1) - Constructs an empty container.
// map();
//
// Constructor (2) - Constructs an empty container.
// explicit map(const Compare& comp,
// const Allocator& alloc = Allocator());
//
// Constructor (3) - Constructs an empty container.
// explicit map(const Allocator& alloc);
//
// 4,5) Constructs the container with the contents of the range [first, last).
// - skipped
//
// Constructor (6) - Copy constructor.
// Constructs the container with the copy of the contents of other.
// map( const map& other );
//
// Constructor (7) - Copy constructor with provided allocator
// map( const map& other, const Allocator& alloc );
//
// Constructor (8) - Move constructor.
// Constructs the container with the contents of other using move semantics.
// map(map&& other);
//
// Constructor (9) - Move constructor with provided allocator
// Constructs the container with the contents of other using move semantics.
// map(map&& other, const Allocator& alloc);
//
// Constructor (10) - Initializer-list constructor.
// Constructs the container with the contents of the initializer list init
// map(std::initializer_list<value_type> init,
// const Compare& comp = Compare(),
// const Allocator& alloc = Allocator());
//
// Constructor (11) - Initializer-list constructor.
// Constructs the container with the contents of the initializer list init
// map(std::initializer_list<value_type> init,
// const Allocator& alloc);
// - skipped
//
// (since C++20)
// (12,13) Constructs the container with the contents of rg.
// - skipped
// Default constructor (1)
// Constructs an empty container with all defaults.
G4AccMap(const G4String& name = "",
G4MergeMode mergeMode = G4MergeMode::kAddition);
// Constructor (2)
// Constructs an empty container with the given compare.
G4AccMap(const Compare& comp,
G4MergeMode mergeMode = G4MergeMode::kAddition,
const Allocator& alloc = Allocator());
// Constructor (2) with name
// Constructs an empty container with the given compare with name
G4AccMap(const G4String& name,
const Compare& comp,
G4MergeMode mergeMode = G4MergeMode::kAddition,
const Allocator& alloc = Allocator());
// Constructor (3)
// Constructs an empty container with the given allocator alloc.
G4AccMap(const Allocator& alloc,
G4MergeMode mergeMode = G4MergeMode::kAddition);
// Constructor (3) with name
// Constructs an empty container with the given allocator alloc and name
G4AccMap(const G4String& name,
const Allocator& alloc,
G4MergeMode mergeMode = G4MergeMode::kAddition);
// Constructor (10)
// Constructs the container with the contents of the initializer list init.
G4AccMap(std::initializer_list<std::pair<const Key,T>> init,
G4MergeMode mergeMode = G4MergeMode::kAddition,
const Compare& comp = Compare(),
const Allocator& alloc = Allocator());
// Constructor (10) with name
// Constructs the container with the contents of the initializer list init.
G4AccMap(const G4String& name,
std::initializer_list<std::pair<const Key,T>> init,
G4MergeMode mergeMode = G4MergeMode::kAddition,
const Compare& comp = Compare(),
const Allocator& alloc = Allocator());
// Copy constructor
G4AccMap(const G4AccMap& rhs) = default;
G4AccMap(const G4AccMap& rhs, const Allocator& allocator);
// Move constructor
G4AccMap(G4AccMap&& rhs) = default;
G4AccMap(G4AccMap&& rhs, const Allocator& allocator);
// Destructor
~G4AccMap() override = default;
// std::map functions, operators
// operator []
inline T& operator[](const Key& key) { return fMap[key]; }
inline T& operator[](Key&& key) { return fMap[std::move(key)]; }
// at
inline T& at(const Key& key) { return fMap[key]; }
inline const T& at(const Key& key ) const { return fMap[key]; }
// size
inline typename std::map<Key, T, Compare, Allocator>::size_type size() const { return fMap.size(); }
// begin, cbegin
inline typename std::map<Key, T, Compare, Allocator>::iterator begin() { return fMap.begin(); }
inline typename std::map<Key, T, Compare, Allocator>::const_iterator begin() const { return fMap.begin(); }
inline typename std::map<Key, T, Compare, Allocator>::const_iterator cbegin() const { return fMap.cbegin(); }
// end, cend
inline typename std::map<Key, T, Compare, Allocator>::iterator end() { return fMap.end(); }
inline typename std::map<Key, T, Compare, Allocator>::const_iterator end() const { return fMap.end(); }
inline typename std::map<Key, T, Compare, Allocator>::const_iterator cend() const { return fMap.cend(); }
// clear
inline void clear() { fMap.clear(); }
// insert
inline std::pair<typename std::map<Key, T, Compare, Allocator>::iterator, bool> insert(const T& value) { return fMap.insert(value); }
template< class P >
inline std::pair<typename std::map<Key, T, Compare, Allocator>::iterator, bool> insert( P&& value ) { return fMap.insert(std::move(value)); }
inline std::pair<typename std::map<Key, T, Compare, Allocator>::iterator, bool> insert( T&& value ) { return fMap.insert(std::move(value)); }
// find
inline typename std::map<Key, T, Compare, Allocator>::iterator find( const Key& key ) { return fMap.find(key); }
inline typename std::map<Key, T, Compare, Allocator>::const_iterator find( const Key& key ) const { return fMap.find(key); }
// Methods
void Merge(const G4VAccumulable& other) final;
void Reset() final;
void Print(G4PrintOptions options = G4PrintOptions()) const final;
void SetMergeMode(G4MergeMode value) final;
void SetInitValue(const T& value);
// Get methods
G4AccType GetType() const final { return G4AccType::kMap; }
std::map<Key, T, Compare, Allocator>& GetMap() { return fMap; }
const std::map<Key, T, Compare, Allocator>& GetMap() const { return fMap; }
private:
// Data members
std::map<Key, T, Compare, Allocator> fMap {};
T fInitValue = 0;
G4MergeFunction<T> fMergeFunction;
};
// inline functions
#include "G4AccMap.icc"
#endif
@@ -0,0 +1,243 @@
//
// ********************************************************************
// * 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, IJCLab IN2P3/CNRS, 19/07/2024
#include <algorithm>
//
// public functions
//
// Default constructor (1)
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
const G4String& name,
G4MergeMode mergeMode)
: G4VAccumulable(name, mergeMode),
fMap(),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccMap ctor1" << G4endl;
}
}
// Constructor (2)
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
const Compare& comp,
G4MergeMode mergeMode,
const Allocator& allocator)
: G4VAccumulable(mergeMode),
fMap(comp, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccMap ctor2" << G4endl;
}
}
// Constructor (2) with name
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
const G4String& name,
const Compare& comp,
G4MergeMode mergeMode,
const Allocator& allocator)
: G4VAccumulable(name, mergeMode),
fMap(comp, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccMap ctor2n" << G4endl;
}
}
// Constructor (3)
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
const Allocator& allocator,
G4MergeMode mergeMode)
: G4VAccumulable(mergeMode),
fMap(allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccMap ctor3" << G4endl;
}
}
// Constructor (3) with name
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
const G4String& name,
const Allocator& allocator,
G4MergeMode mergeMode)
: G4VAccumulable(name, mergeMode),
fMap(allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccMap ctor3n" << G4endl;
}
}
// Constructor (10)
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
std::initializer_list<std::pair<const Key,T>> init,
G4MergeMode mergeMode,
const Compare& comp,
const Allocator& allocator)
: G4VAccumulable(mergeMode),
fMap(init, comp, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccMap ctor10" << G4endl;
}
}
// Constructor (10) with name
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
const G4String& name,
std::initializer_list<std::pair<const Key,T>> init,
G4MergeMode mergeMode,
const Compare& comp,
const Allocator& allocator)
: G4VAccumulable(name, mergeMode),
fMap(init, comp, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccMap ctor10n" << G4endl;
}
}
// Copy ctor
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
const G4AccMap& rhs,
const Allocator& allocator)
: G4VAccumulable(rhs),
fMap(rhs, allocator),
fMergeFunction(rhs.fMergeFunction)
{}
// Move ctor
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
G4AccMap&& rhs,
const Allocator& allocator)
: G4VAccumulable(std::move(rhs)),
fMap(std::move(rhs), allocator),
fMergeFunction(rhs.fMergeFunction)
{}
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
void G4AccMap<Key, T, Compare, Allocator>::Merge(const G4VAccumulable& other)
{
const auto& otherMap = static_cast<const G4AccMap<Key, T, Compare, Allocator>&>(other);
if (G4Accumulables::VerboseLevel > 2 ) {
G4cout << "G4AccMap<Key, T, Compare, Allocator>::Merge: " << G4endl;
G4cout << "destination: ";
for (const auto& [key, v] : fMap) {
G4cout << "[ " << key << ", " << v << " ], ";
}
G4cout << G4endl;
G4cout << "merged data: ";
for (const auto& [key, v] : otherMap.fMap) {
G4cout << "[ " << key << ", " << v << " ], ";
}
G4cout << G4endl;
}
for (const auto& [key, value] : otherMap.fMap) {
if ( fMap.find(key) == fMap.end()) {
fMap[key] = value;
}
else {
fMap[key] = fMergeFunction(fMap[key], value);
}
}
}
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
void G4AccMap<Key, T, Compare, Allocator>::Reset()
{
for (auto& [key, value] :fMap) {
fMap[key] = fInitValue;
}
}
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
void G4AccMap<Key, T, Compare, Allocator>::Print(
G4PrintOptions options) const
{
if (options.Has(G4PrintOptions::kType)) {
G4cout << "map<" << typeid(Key).name() << ", " << typeid(T).name() << ">: ";
}
PrintBase(options);
bool first = true;
for (const auto& [key, value] : fMap) {
if (! first) { G4cout << ", "; }
G4cout << "[ " << key << ", " << value << "] ";
first = false;
}
G4cout << G4endl;
}
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
void G4AccMap<Key, T, Compare, Allocator>::SetMergeMode(G4MergeMode value)
{
G4VAccumulable::SetMergeMode(value);
fMergeFunction = GetMergeFunction<T>(fMergeMode);
}
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
void G4AccMap<Key, T, Compare, Allocator>::SetInitValue(const T& value)
{
fInitValue = value;
}
@@ -0,0 +1,98 @@
//
// ********************************************************************
// * 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, IJCLab IN2P3/CNRS, 25/07/2024
#ifndef G4AccType_h
#define G4AccType_h 1
#include "globals.hh"
#include <bitset>
// Enumeration for definition available accummulables
enum class G4AccType {
kValue, // G4AccValue<T>
kArray, // G4AccArray<T>
kMap, // G4AccMap<T>
kUnorderedMap, // G4AccUnorderedMap<T>
kVector, // G4AccumulableVector<T>
kUser // User type
};
// TODO: add G4String GetTypeName(G4AccType)
// Helper class for printing
//
class G4PrintOptions {
public:
enum Option : std::size_t {
kName = 0,
kType = 1,
kId = 2
};
G4PrintOptions() {
SetDefaults();
}
G4PrintOptions(std::initializer_list<Option> options) {
SetDefaults();
for (const Option& option : options) {
fValue.set(option);
}
}
~G4PrintOptions() = default;
void Set(Option option, bool value = true) { fValue.set(option, value); }
bool Has(Option option) const { return fValue[option]; }
private:
// methods
void SetDefaults() {
fValue.set(kName);
fValue.set(kType);
}
// data members
static constexpr std::size_t kMax = G4PrintOptions::kId + 1;
std::bitset<kMax> fValue{0};
};
namespace G4Accumulables
{
// Constant expressions
//
constexpr G4int kInvalidId { -1 };
// Verbose level
//
[[maybe_unused]] static G4int VerboseLevel {1};
}
#endif
@@ -0,0 +1,257 @@
//
// ********************************************************************
// * 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. *
// ********************************************************************
//
// Class template for accumulable unordered_maps handled by Geant4 analysis
//
// Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 19/07/2024
#ifndef G4AccUnorderedMap_h
#define G4AccUnorderedMap_h 1
#include "G4VAccumulable.hh"
#include "G4MergeMode.hh"
#include "globals.hh"
#include <unordered_map>
template <class Key,
class T,
class Hash = std::hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Allocator = std::allocator<std::pair<const Key, T>>>
class G4AccUnorderedMap : public G4VAccumulable
{
public:
// ctors to be supported
// (https://en.cppreference.com/w/cpp/container/unordered_map/unordered_map)
//
// Default constructor (1) - Constructs an empty container.
// unordered_map();
//
// Constructor (2) - Constructs an empty container.
// explicit unordered_map( size_type bucket_count,
// const Hash& hash = Hash(),
// const KeyEqual& equal = KeyEqual(),
// const Allocator& alloc = Allocator() );
//
// Constructor (3) - Constructs an empty container.
// unordered_map( size_type bucket_count,
// const Allocator& alloc )
// : unordered_map(bucket_count, Hash(), KeyEqual(), alloc) {}
//
// Constructor (4) (since C++14)
// Constructs empty container.
// unordered_map( size_type bucket_count,
// const Hash& hash,
// const Allocator& alloc )
// : unordered_map(bucket_count, hash, KeyEqual(), alloc) {}
//
// Constructor (5) (since C++11)
// Constructs empty container.
// explicit unordered_map( const Allocator& alloc );
//
// (6,7,8) (since C++11)
// Constructs the container with the contents of the range
// - skipped
//
// (9) (since C++11) - Copy constructor.
// unordered_map( const unordered_map& other );
//
// (10) (since C++11) - Copy constructor with provided allocator
// unordered_map( const unordered_map& other, const Allocator& alloc );
//
// (11) (since C++11) - Move constructor.
// unordered_map( unordered_map&& other );
//
// (12) (since C++11) - Move constructor with provided allocator
// unordered_map( unordered_map&& other, const Allocator& alloc );
//
// (13) (since C++11) - Initializer-list constructor.
// Constructs the container with the contents of the initializer list init
// unordered_map( std::initializer_list<value_type> init,
// size_type bucket_count = /* implementation-defined */,
// const Hash& hash = Hash(),
// const KeyEqual& equal = KeyEqual(),
// const Allocator& alloc = Allocator() );
//
// (14) (since C++11) - Initializer-list constructor.
// Constructs the container with the contents of the initializer list init
// unordered_map( std::initializer_list<value_type> init,
// size_type bucket_count,
// const Allocator& alloc )
// : unordered_map(init, bucket_count,
// Hash(), KeyEqual(), alloc) {}
//
// (15) (since C++14) - Initializer-list constructor.
// unordered_map( std::initializer_list<value_type> init,
// size_type bucket_count,
// const Hash& hash,
// const Allocator& alloc )
// : unordered_map(init, bucket_count,
// hash, KeyEqual(), alloc) {}
//
// (16,17) (since C++23)
// Constructs the container with the contents of rg.
// - skipped
// Default constructor (1)
// Constructs an empty container with all defaults.
G4AccUnorderedMap(const G4String& name = "",
G4MergeMode mergeMode = G4MergeMode::kAddition);
// Constructor (2)
// Constructs an empty container with the given bucket_count
G4AccUnorderedMap(std::size_t bucket_count,
G4MergeMode mergeMode = G4MergeMode::kAddition,
const Allocator& alloc = Allocator());
// Constructor (2) with name
// Constructs an empty container with the given bucket_count and name
G4AccUnorderedMap(const G4String& name,
std::size_t bucket_count,
G4MergeMode mergeMode = G4MergeMode::kAddition,
const Allocator& alloc = Allocator());
// Constructor (3)
// Constructs an empty container with the given bucket_count and allocator
G4AccUnorderedMap(std::size_t bucket_count,
const Allocator& alloc,
G4MergeMode mergeMode = G4MergeMode::kAddition);
// Constructor (3) with name
// Constructs an empty container with the given bucket_count, allocator and name
G4AccUnorderedMap(const G4String& name,
std::size_t bucket_count,
const Allocator& alloc,
G4MergeMode mergeMode = G4MergeMode::kAddition);
// Constructor (4)
// Constructs an empty container with the given bucket_count, allocator and hash
G4AccUnorderedMap(std::size_t bucket_count,
const Hash& hash,
const Allocator& alloc,
G4MergeMode mergeMode = G4MergeMode::kAddition);
// Constructor (4) with name
// Constructs an empty container with the given bucket_count, allocator, hash and name
G4AccUnorderedMap(const G4String& name,
std::size_t bucket_count,
const Hash& hash,
const Allocator& alloc,
G4MergeMode mergeMode = G4MergeMode::kAddition);
// Constructor (5)
// Constructs an empty container with the given allocator alloc.
G4AccUnorderedMap(const Allocator& alloc,
G4MergeMode mergeMode = G4MergeMode::kAddition);
// Constructor (5) with name
// Constructs an empty container with the given allocator alloc and name
G4AccUnorderedMap(const G4String& name,
const Allocator& alloc,
G4MergeMode mergeMode = G4MergeMode::kAddition);
// Constructor (13)
// Constructs the container with the contents of the initializer list init.
G4AccUnorderedMap(std::initializer_list<std::pair<const Key,T>> init,
G4MergeMode mergeMode = G4MergeMode::kAddition,
std::size_t bucket_count = 0,
const Hash& hash = Hash(),
const KeyEqual& equal = KeyEqual(),
const Allocator& alloc = Allocator() );
// Constructor (13) with name
// Constructs the container with the contents of the initializer list init.
G4AccUnorderedMap(const G4String& name,
std::initializer_list<std::pair<const Key,T>> init,
G4MergeMode mergeMode = G4MergeMode::kAddition,
std::size_t bucket_count = 0,
const Hash& hash = Hash(),
const KeyEqual& equal = KeyEqual(),
const Allocator& alloc = Allocator() );
// Copy constructor
G4AccUnorderedMap(const G4AccUnorderedMap& rhs) = default;
G4AccUnorderedMap(const G4AccUnorderedMap& rhs, const Allocator& allocator);
// Move constructor
G4AccUnorderedMap(G4AccUnorderedMap&& rhs) = default;
G4AccUnorderedMap(G4AccUnorderedMap&& rhs, const Allocator& allocator);
// Destructor
~G4AccUnorderedMap() override = default;
// std::unordered_map functions, operators
// operator []
inline T& operator[](const Key& key) { return fUMap[key]; }
inline T& operator[](Key&& key) { return fUMap[std::move(key)]; }
// at
inline T& at(const Key& key) { return fUMap[key]; }
inline const T& at(const Key& key ) const { return fUMap[key]; }
// size
inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::size_type size() const { return fUMap.size(); }
// begin, cbegin
inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator begin() { return fUMap.begin(); }
inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::const_iterator begin() const { return fUMap.begin(); }
inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::const_iterator cbegin() const { return fUMap.cbegin(); }
// end, cend
inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator end() { return fUMap.end(); }
inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::const_iterator end() const { return fUMap.end(); }
inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::const_iterator cend() const { return fUMap.cend(); }
// clear
inline void clear() { fUMap.clear(); }
// insert
inline std::pair<typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator, bool> insert(const T& value) { return fUMap.insert(value); }
template< class P >
inline std::pair<typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator, bool> insert( P&& value ) { return fUMap.insert(std::move(value)); }
inline std::pair<typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator, bool> insert( T&& value ) { return fUMap.insert(std::move(value)); }
// find
inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator find( const Key& key ) { return fUMap.find(key); }
inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::const_iterator find( const Key& key ) const { return fUMap.find(key); }
// Methods
void Merge(const G4VAccumulable& other) final;
void Reset() final;
void Print(G4PrintOptions options = G4PrintOptions()) const final;
void SetMergeMode(G4MergeMode value) final;
void SetInitValue(const T& value);
// Get methods
G4AccType GetType() const final { return G4AccType::kUnorderedMap; }
std::unordered_map<Key, T, Hash, KeyEqual, Allocator>& GetUnorderedMap() { return fUMap; }
const std::unordered_map<Key, T, Hash, KeyEqual, Allocator>& GetUnorderedMap() const { return fUMap; }
private:
// Data members
std::unordered_map<Key, T, Hash, KeyEqual, Allocator> fUMap;
T fInitValue = 0;
G4MergeFunction<T> fMergeFunction;
};
// inline functions
#include "G4AccUnorderedMap.icc"
#endif
@@ -0,0 +1,315 @@
//
// ********************************************************************
// * 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, IJCLab IN2P3/CNRS, 19/07/2024
#include <algorithm>
//
// public functions
//
// Default constructor (1)
//_____________________________________________________________________________
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
const G4String& name,
G4MergeMode mergeMode)
: G4VAccumulable(name, mergeMode),
fUMap(),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccUnorderedMap ctor1" << G4endl;
}
}
// Constructor (2)
//_____________________________________________________________________________
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
std::size_t bucket_count,
G4MergeMode mergeMode,
const Allocator& allocator)
: G4VAccumulable(mergeMode),
fUMap(bucket_count, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccUnorderedMap ctor2" << G4endl;
}
}
// Constructor (2) with name
//_____________________________________________________________________________
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
const G4String& name,
std::size_t bucket_count,
G4MergeMode mergeMode,
const Allocator& allocator)
: G4VAccumulable(name, mergeMode),
fUMap(bucket_count, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccUnorderedMap ctor2n" << G4endl;
}
}
// Constructor (3)
//_____________________________________________________________________________
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
std::size_t bucket_count,
const Allocator& allocator,
G4MergeMode mergeMode)
: G4VAccumulable(mergeMode),
fUMap(bucket_count, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccUnorderedMap ctor3" << G4endl;
}
}
// Constructor (3) with name
//_____________________________________________________________________________
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
const G4String& name,
std::size_t bucket_count,
const Allocator& allocator,
G4MergeMode mergeMode)
: G4VAccumulable(name, mergeMode),
fUMap(bucket_count, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccUnorderedMap ctor3n" << G4endl;
}
}
// Constructor (4)
//_____________________________________________________________________________
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
std::size_t bucket_count,
const Hash& hash,
const Allocator& allocator,
G4MergeMode mergeMode)
: G4VAccumulable(mergeMode),
fUMap(bucket_count, hash, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccUnorderedMap ctor4" << G4endl;
}
}
// Constructor (4) with name
//_____________________________________________________________________________
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
const G4String& name,
std::size_t bucket_count,
const Hash& hash,
const Allocator& allocator,
G4MergeMode mergeMode)
: G4VAccumulable(name, mergeMode),
fUMap(bucket_count, hash, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccUnorderedMap ctor4n" << G4endl;
}
}
// Constructor (5)
//_____________________________________________________________________________
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
const Allocator& allocator,
G4MergeMode mergeMode)
: G4VAccumulable(mergeMode),
fUMap(allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccUnorderedMap ctor5" << G4endl;
}
}
// Constructor (5) with name
//_____________________________________________________________________________
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
const G4String& name,
const Allocator& allocator,
G4MergeMode mergeMode)
: G4VAccumulable(name, mergeMode),
fUMap(allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccUnorderedMap ctor5n" << G4endl;
}
}
// Constructor (13)
//_____________________________________________________________________________
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
std::initializer_list<std::pair<const Key,T>> init,
G4MergeMode mergeMode,
std::size_t bucket_count,
const Hash& hash,
const KeyEqual& equal,
const Allocator& allocator)
: G4VAccumulable(mergeMode),
fUMap(init, bucket_count, hash, equal, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccUnorderedMap ctor13" << G4endl;
}
}
// Constructor (13) with name
//_____________________________________________________________________________
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
const G4String& name,
std::initializer_list<std::pair<const Key,T>> init,
G4MergeMode mergeMode,
std::size_t bucket_count,
const Hash& hash,
const KeyEqual& equal,
const Allocator& allocator)
: G4VAccumulable(name, mergeMode),
fUMap(init, bucket_count, hash, equal, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccUnorderedMap ctor13n" << G4endl;
}
}
// Copy ctor
//_____________________________________________________________________________
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
const G4AccUnorderedMap& rhs,
const Allocator& allocator)
: G4VAccumulable(rhs),
fUMap(rhs, allocator),
fMergeFunction(rhs.fMergeFunction)
{}
// Move ctor
//_____________________________________________________________________________
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
G4AccUnorderedMap&& rhs,
const Allocator& allocator)
: G4VAccumulable(std::move(rhs)),
fUMap(std::move(rhs), allocator),
fMergeFunction(rhs.fMergeFunction)
{}
//_____________________________________________________________________________
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
void G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::Merge(const G4VAccumulable& other)
{
const auto& otherMap = static_cast<const G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>&>(other);
if (G4Accumulables::VerboseLevel > 2 ) {
G4cout << "G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::Merge: " << G4endl;
G4cout << "destination: ";
for (const auto& [key, v] : fUMap) {
G4cout << "[ " << key << ", " << v << " ], ";
}
G4cout << G4endl;
G4cout << "merged data: ";
for (const auto& [key, v] : otherMap.fUMap) {
G4cout << "[ " << key << ", " << v << " ], ";
}
G4cout << G4endl;
}
for (const auto& [key, value] : otherMap.fUMap) {
if ( fUMap.find(key) == fUMap.end()) {
(fUMap)[key] = value;
}
else {
(fUMap)[key] = fMergeFunction((fUMap)[key], value);
}
}
}
//_____________________________________________________________________________
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
void G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::Reset()
{
for (auto& [key, value] : fUMap) {
fUMap[key] = fInitValue;
}
}
//_____________________________________________________________________________
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
void G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::Print(
G4PrintOptions options) const
{
if (options.Has(G4PrintOptions::kType)) {
G4cout << "unordered_map<" << typeid(Key).name() << ", " << typeid(T).name() << ">: ";
}
PrintBase(options);
bool first = true;
for (const auto& [key, value] : fUMap) {
if (! first) { G4cout << ", "; }
G4cout << "[ " << key << ", " << value << "] ";
first = false;
}
G4cout << G4endl;
}
//_____________________________________________________________________________
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
void G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::SetMergeMode(G4MergeMode value)
{
G4VAccumulable::SetMergeMode(value);
fMergeFunction = GetMergeFunction<T>(fMergeMode);
}
//_____________________________________________________________________________
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
void G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::SetInitValue(const T& value)
{
fInitValue = value;
}
@@ -0,0 +1,88 @@
//
// ********************************************************************
// * 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. *
// ********************************************************************
//
// Class template for accumulable values handled by Geant4 analysis
//
// Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 07/09/2015
#ifndef G4AccValue_h
#define G4AccValue_h 1
#include "G4VAccumulable.hh"
#include "G4MergeMode.hh"
#include "globals.hh"
template <typename T>
class G4AccValue : public G4VAccumulable
{
public:
G4AccValue(const G4String& name, T initValue,
G4MergeMode mergeMode = G4MergeMode::kAddition);
G4AccValue(T initValue = 0,
G4MergeMode mergeMode = G4MergeMode::kAddition);
G4AccValue(const G4AccValue& rhs);
G4AccValue(G4AccValue&& rhs) noexcept;
~G4AccValue() override = default;
// Operators
G4AccValue<T>& operator= (const G4AccValue<T>& rhs);
G4AccValue<T>& operator=(G4AccValue<T>&& rhs) noexcept;
G4AccValue<T>& operator+=(const G4AccValue<T>& rhs);
G4AccValue<T>& operator*=(const G4AccValue<T>& rhs);
G4AccValue<T> operator++(int); // postfix increment
G4AccValue<T>& operator++(); // prefix increment
G4AccValue<T>& operator= (const T& rhs);
G4AccValue<T>& operator+=(const T& rhs);
G4AccValue<T>& operator*=(const T& rhs);
// Methods
void Merge(const G4VAccumulable& other) final;
void Reset() final;
using G4VAccumulable::Print;
void Print(G4PrintOptions options = G4PrintOptions()) const final;
// using G4VAccumulable::SetMergeMode;
void SetMergeMode(G4MergeMode value) final;
G4AccType GetType() const final { return G4AccType::kValue; }
// Get methods
T GetValue() const;
private:
// Data members
T fValue = 0;
T fInitValue = 0;
G4MergeFunction<T> fMergeFunction;
};
// inline functions
#include "G4AccValue.icc"
#endif
@@ -24,9 +24,7 @@
// ********************************************************************
//
//
// utilities
//
// Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 07/09/2015
//
// public functions
@@ -36,51 +34,50 @@ using namespace G4Accumulables;
//_____________________________________________________________________________
template <typename T>
G4Accumulable<T>::G4Accumulable(const G4String& name, T initValue, G4MergeMode mergeMode)
: G4VAccumulable(name),
G4AccValue<T>::G4AccValue(
const G4String& name, T initValue, G4MergeMode mergeMode)
: G4VAccumulable(name, mergeMode),
fValue(initValue),
fInitValue(initValue),
fMergeMode(mergeMode),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{}
//_____________________________________________________________________________
template <typename T>
G4Accumulable<T>::G4Accumulable(T initValue, G4MergeMode mergeMode)
G4AccValue<T>::G4AccValue(T initValue, G4MergeMode mergeMode)
: G4VAccumulable(),
fValue(initValue),
fInitValue(initValue),
fMergeMode(mergeMode),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{}
//_____________________________________________________________________________
template <typename T>
G4Accumulable<T>::G4Accumulable(const G4Accumulable& rhs)
G4AccValue<T>::G4AccValue(const G4AccValue& rhs)
: G4VAccumulable(rhs),
fValue(rhs.fValue),
fInitValue(rhs.fInitValue),
fMergeMode(rhs.fMergeMode),
fMergeFunction(rhs.fMergeFunction)
{}
//_____________________________________________________________________________
template <typename T>
G4Accumulable<T>::G4Accumulable(G4Accumulable&& rhs) noexcept
: G4VAccumulable(rhs),
G4AccValue<T>::G4AccValue(G4AccValue&& rhs) noexcept
: G4VAccumulable(std::move(rhs)),
fValue(std::move(rhs.fValue)),
fInitValue(std::move(rhs.fInitValue)),
fMergeMode(rhs.fMergeMode),
fMergeFunction(rhs.fMergeFunction)
{}
//_____________________________________________________________________________
template <typename T>
G4Accumulable<T>&
G4Accumulable<T>::operator=(const G4Accumulable<T>& rhs)
G4AccValue<T>&
G4AccValue<T>::operator=(const G4AccValue<T>& rhs)
{
// check assignment to self
if (this == &rhs) return *this;
if (this == &rhs) {
return *this;
}
// base class assignment
G4VAccumulable::operator=(rhs);
@@ -88,7 +85,6 @@ G4Accumulable<T>::operator=(const G4Accumulable<T>& rhs)
// this class data assignment
fValue = rhs.fValue;
fInitValue = rhs.fInitValue;
fMergeMode = rhs.fMergeMode;
fMergeFunction = rhs.fMergeFunction;
return *this;
@@ -96,10 +92,12 @@ G4Accumulable<T>::operator=(const G4Accumulable<T>& rhs)
//_____________________________________________________________________________
template <typename T>
G4Accumulable<T>& G4Accumulable<T>::operator=(G4Accumulable<T>&& rhs) noexcept
G4AccValue<T>& G4AccValue<T>::operator=(G4AccValue<T>&& rhs) noexcept
{
// check assignment to self
if (this == &rhs) return *this;
if (this == &rhs) {
return *this;
}
// base class assignment
G4VAccumulable::operator=(rhs);
@@ -107,7 +105,6 @@ G4Accumulable<T>& G4Accumulable<T>::operator=(G4Accumulable<T>&& rhs) noexcept
// this class data assignment
fValue = std::move(rhs.fValue);
fInitValue = std::move(rhs.fInitValue);
fMergeMode = rhs.fMergeMode;
fMergeFunction = rhs.fMergeFunction;
return *this;
@@ -115,8 +112,8 @@ G4Accumulable<T>& G4Accumulable<T>::operator=(G4Accumulable<T>&& rhs) noexcept
//_____________________________________________________________________________
template <typename T>
G4Accumulable<T>&
G4Accumulable<T>::operator+=(const G4Accumulable<T>& rhs)
G4AccValue<T>&
G4AccValue<T>::operator+=(const G4AccValue<T>& rhs)
{
// only update the value from rhs
fValue += rhs.fValue;
@@ -125,8 +122,8 @@ G4Accumulable<T>::operator+=(const G4Accumulable<T>& rhs)
//_____________________________________________________________________________
template <typename T>
G4Accumulable<T>&
G4Accumulable<T>::operator*=(const G4Accumulable<T>& rhs)
G4AccValue<T>&
G4AccValue<T>::operator*=(const G4AccValue<T>& rhs)
{
// only update the value from rhs
fValue *= rhs.fValue;
@@ -135,8 +132,8 @@ G4Accumulable<T>::operator*=(const G4Accumulable<T>& rhs)
//_____________________________________________________________________________
template <typename T>
G4Accumulable<T>&
G4Accumulable<T>::operator=(const T& value)
G4AccValue<T>&
G4AccValue<T>::operator=(const T& value)
{
// only update the value
fValue = value;
@@ -145,8 +142,8 @@ G4Accumulable<T>::operator=(const T& value)
//_____________________________________________________________________________
template <typename T>
G4Accumulable<T>&
G4Accumulable<T>::operator+=(const T& value)
G4AccValue<T>&
G4AccValue<T>::operator+=(const T& value)
{
// only update the value
fValue += value;
@@ -155,8 +152,8 @@ G4Accumulable<T>::operator+=(const T& value)
//_____________________________________________________________________________
template <typename T>
G4Accumulable<T>&
G4Accumulable<T>::operator*=(const T& value)
G4AccValue<T>&
G4AccValue<T>::operator*=(const T& value)
{
// only update the value from rhs
fValue *= value;
@@ -165,19 +162,19 @@ G4Accumulable<T>::operator*=(const T& value)
//_____________________________________________________________________________
template <typename T>
G4Accumulable<T>
G4Accumulable<T>::operator++(int)
G4AccValue<T>
G4AccValue<T>::operator++(int)
{
// postfix increment
G4Accumulable<T> temp = *this;
G4AccValue<T> temp = *this;
fValue++;
return temp;
}
//_____________________________________________________________________________
template <typename T>
G4Accumulable<T>&
G4Accumulable<T>::operator++()
G4AccValue<T>&
G4AccValue<T>::operator++()
{
// prefix increment
fValue++;
@@ -186,24 +183,52 @@ G4Accumulable<T>::operator++()
//_____________________________________________________________________________
template <typename T>
void G4Accumulable<T>::Merge(const G4VAccumulable& other)
void G4AccValue<T>::Merge(const G4VAccumulable& other)
{
// G4cout << "Merging other: " << other.GetName() << " " << static_cast<const G4Accumulable<T>&>(other).fValue << G4endl;
// G4cout << " to master: " << fName << " " << fValue << G4endl;
fValue = fMergeFunction(fValue, static_cast<const G4Accumulable<T>&>(other).fValue);
// G4cout << " new value: " << fName << " " << fValue << G4endl;
if (G4Accumulables::VerboseLevel > 2 ) {
G4cout << "Merging other: " << other.GetName() << " "
<< static_cast<const G4AccValue<T>&>(other).fValue << G4endl;
G4cout << " to master: " << fName << " " << fValue << G4endl;
}
fValue = fMergeFunction(fValue, static_cast<const G4AccValue<T>&>(other).fValue);
if (G4Accumulables::VerboseLevel > 2 ) {
G4cout << " new value: " << fName << " " << fValue << G4endl;
}
}
//_____________________________________________________________________________
template <typename T>
void G4Accumulable<T>::Reset()
void G4AccValue<T>::Reset()
{
fValue = fInitValue;
}
//_____________________________________________________________________________
template <typename T>
T G4Accumulable<T>::GetValue() const
void G4AccValue<T>::Print(G4PrintOptions options) const
{
if (options.Has(G4PrintOptions::kType)) {
G4cout << typeid(T).name() << ": ";
}
PrintBase(options);
G4cout << fValue << G4endl;
}
//_____________________________________________________________________________
template <class T>
void G4AccValue<T>::SetMergeMode(G4MergeMode value)
{
G4VAccumulable::SetMergeMode(value);
fMergeFunction = GetMergeFunction<T>(fMergeMode);
}
//_____________________________________________________________________________
template <typename T>
T G4AccValue<T>::GetValue() const
{
return fValue;
}
@@ -0,0 +1,196 @@
//
// ********************************************************************
// * 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. *
// ********************************************************************
//
// Class template for accumulable vectors handled by Geant4 analysis
//
// Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 12/07/2024
#ifndef G4AccVector_h
#define G4AccVector_h 1
#include "G4VAccumulable.hh"
#include "G4MergeMode.hh"
#include "globals.hh"
#include <vector>
// using vector_std::size_t = std::vector::std::size_t;
template <class T, class Allocator = std::allocator<T>>
class G4AccVector : public G4VAccumulable
{
public:
// ctors to be supported
// (https://en.cppreference.com/w/cpp/container/vector/vector)
// 1) Default constructor. Constructs an empty container with a default-constructed allocator.
// vector() noexcept(noexcept(Allocator()));
// 3) Constructs the container with count copies of elements with value value.
// vector( std::size_t count,
// const T& value,
// const Allocator& alloc = Allocator() );
// 4) Constructs the container with count default-inserted instances of T. No copies are made.
// vector( std::size_t count,
// const Allocator& alloc = Allocator() );
// 6) Copy constructor. Constructs the container with the copy of the contents of other.
// vector( const vector& other );
// 8) Move constructor. Constructs the container with the contents of other using move semantics.
// vector( vector&& other );
// 10) Constructs the container with the contents of the initializer list init.
// vector( std::initializer_list<T> init,
// const Allocator& alloc = Allocator() );
// Default constructor (1)
// Constructs an empty container with a default-constructed allocator.
G4AccVector(const G4String& name = "",
G4MergeMode mergeMode = G4MergeMode::kAddition);
// Constructor (2)
// Constructs an empty container with the given allocator alloc.
G4AccVector(const Allocator& alloc,
G4MergeMode mergeMode = G4MergeMode::kAddition);
// Constructor (2) with name
// Constructs an empty container with the given allocator alloc.
G4AccVector(const G4String& name,
const Allocator& alloc,
G4MergeMode mergeMode = G4MergeMode::kAddition);
// Constructor (3)
// Constructs the container with count copies of elements with value value
// with a default-constructed allocator.
// G4AccVector(std::size_t count, const T& value,
// const Allocator& alloc = Allocator());
G4AccVector(std::size_t count, const T& value,
G4MergeMode mergeMode = G4MergeMode::kAddition,
const Allocator& allocator = Allocator());
// Constructor (3) with name
// Constructs the container with count copies of elements with value value
// with a default-constructed allocator.
// G4AccVector(std::size_t count, const T& value,
// const Allocator& alloc = Allocator());
G4AccVector(const G4String& name,
std::size_t count, const T& value,
G4MergeMode mergeMode = G4MergeMode::kAddition,
const Allocator& allocator = Allocator());
// Constructor (4)
// Constructs the container with count default-inserted instances of T.
// No copies are made.
// G4AccVector(std::size_t count,
// const Allocator& alloc = Allocator() );
G4AccVector(std::size_t count,
G4MergeMode mergeMode = G4MergeMode::kAddition,
const Allocator& allocator = Allocator());
// Constructor (4) with name
// Constructs the container with count default-inserted instances of T.
// No copies are made.
// G4AccVector(std::size_t count,
// const Allocator& alloc = Allocator() );
G4AccVector(const G4String& name,
std::size_t count,
G4MergeMode mergeMode = G4MergeMode::kAddition,
const Allocator& allocator = Allocator());
// Constructor (10)
// Constructs the container with the contents of the initializer list init.
// G4AccVector(std::initializer_list<T> init,
// const Allocator& alloc = Allocator() );
G4AccVector(std::initializer_list<T> init,
G4MergeMode mergeMode = G4MergeMode::kAddition,
const Allocator& allocator = Allocator());
// Constructor (10) with name
// Constructs the container with the contents of the initializer list init.
// G4AccVector(std::initializer_list<T> init,
// const Allocator& alloc = Allocator() );
G4AccVector(const G4String& name,
std::initializer_list<T> init,
G4MergeMode mergeMode = G4MergeMode::kAddition,
const Allocator& allocator = Allocator());
// Copy constructor
G4AccVector(const G4AccVector& rhs) = default;
G4AccVector(const G4AccVector& rhs, const Allocator& allocator);
// Move constructor
G4AccVector(G4AccVector&& rhs) = default;
G4AccVector(G4AccVector&& rhs, const Allocator& allocator);
// Destructor
~G4AccVector() override = default;
// std::vector functions, operators
// operator []
inline T& operator[](typename std::vector<T>::size_type i) { return fVector[i]; }
// at
inline T& at(typename std::vector<T>::size_type i) { return fVector.at(i); }
// size
inline typename std::vector<T>::size_type size() const { return fVector.size(); }
// begin, cbegin
inline typename std::vector<T>::iterator begin() { return fVector.begin(); }
inline typename std::vector<T>::const_iterator begin() const { return fVector.begin(); }
inline typename std::vector<T>::const_iterator cbegin() const { return fVector.cbegin(); }
// end, cend
inline typename std::vector<T>::iterator end() { return fVector.end(); }
inline typename std::vector<T>::const_iterator end() const { return fVector.end(); }
inline typename std::vector<T>::const_iterator cend() const { return fVector.cend(); }
// clear
inline void clear() { fVector.clear(); }
// push_back
inline void push_back(const T& value) { fVector.push_back(value); }
inline void push_back(T&& value ) { fVector.push_back(std::move(value)); }
// emplace_back
template< class... Args >
inline void emplace_back(Args&&... args ) { fVector.emplace_back(args...); }
template< class... Args >
inline T& emplace_back(Args&&... args ) { return fVector.emplace_back(args...); }
// pop_back
inline void pop_back() { fVector.pop_back(); }
// Methods
void Merge(const G4VAccumulable& other) final;
void Reset() final;
void Print(G4PrintOptions options = G4PrintOptions()) const final;
void SetMergeMode(G4MergeMode value) final;
// Get methods
G4AccType GetType() const final { return G4AccType::kVector; }
std::vector<T>& GetVector();
const std::vector<T>& GetVector() const;
private:
// Data members
std::vector<T> fVector {};
T fInitValue = 0;
G4MergeFunction<T> fMergeFunction;
};
// inline functions
#include "G4AccVector.icc"
#endif
@@ -0,0 +1,254 @@
//
// ********************************************************************
// * 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, IJCLab IN2P3/CNRS, 12/07/2024
#include <algorithm>
//
// public functions
//
// Default constructor (1)
//_____________________________________________________________________________
template <class T, class Allocator>
G4AccVector<T, Allocator>::G4AccVector(
const G4String& name,
G4MergeMode mergeMode)
: G4VAccumulable(name, mergeMode),
fVector(),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccVector ctor1" << G4endl;
}
}
// Constructor (2)
//_____________________________________________________________________________
template <class T, class Allocator>
G4AccVector<T, Allocator>::G4AccVector(
const Allocator& allocator,
G4MergeMode mergeMode)
: G4VAccumulable(mergeMode),
fVector(allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccVector ctor2" << G4endl;
}
}
// Constructor (2) with name
//_____________________________________________________________________________
template <class T, class Allocator>
G4AccVector<T, Allocator>::G4AccVector(
const G4String& name,
const Allocator& allocator,
G4MergeMode mergeMode)
: G4VAccumulable(name, mergeMode),
fVector(allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccVector ctor2n" << G4endl;
}
}
// Constructor (3)
//_____________________________________________________________________________
template <class T, class Allocator>
G4AccVector<T, Allocator>::G4AccVector(
std::size_t count, const T& value,
G4MergeMode mergeMode, const Allocator& allocator)
: G4VAccumulable(mergeMode),
fVector(count, value, allocator),
fInitValue(value),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccVector ctor3" << G4endl;
}
}
// Constructor (3) with name
//_____________________________________________________________________________
template <class T, class Allocator>
G4AccVector<T, Allocator>::G4AccVector(
const G4String& name,
std::size_t count, const T& value,
G4MergeMode mergeMode, const Allocator& allocator)
: G4VAccumulable(name, mergeMode),
fVector(count, value, allocator),
fInitValue(value),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccVector ctor3n" << G4endl;
}
}
// Constructor (4)
//_____________________________________________________________________________
template <class T, class Allocator>
G4AccVector<T, Allocator>::G4AccVector(
std::size_t count,
G4MergeMode mergeMode, const Allocator& allocator)
: G4VAccumulable(mergeMode),
fVector(count, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccVector ctor4" << G4endl;
}
}
// Constructor (4) with name
//_____________________________________________________________________________
template <class T, class Allocator>
G4AccVector<T, Allocator>::G4AccVector(
const G4String& name,
std::size_t count,
G4MergeMode mergeMode, const Allocator& allocator)
: G4VAccumulable(name, mergeMode),
fVector(count, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccVector ctor4n" << G4endl;
}
}
// Constructor (10)
//_____________________________________________________________________________
template <class T, class Allocator>
G4AccVector<T, Allocator>::G4AccVector(
std::initializer_list<T> init,
G4MergeMode mergeMode, const Allocator& allocator)
: G4VAccumulable(mergeMode),
fVector(init, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccVector ctor10" << G4endl;
}
}
// Constructor (10) with name
//_____________________________________________________________________________
template <class T, class Allocator>
G4AccVector<T, Allocator>::G4AccVector(
const G4String& name,
std::initializer_list<T> init,
G4MergeMode mergeMode, const Allocator& allocator)
: G4VAccumulable(name, mergeMode),
fVector(init, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccVector ctor10n" << G4endl;
}
}
// Copy ctor
//_____________________________________________________________________________
template <class T, class Allocator>
G4AccVector<T, Allocator>::G4AccVector(
const G4AccVector& rhs, const Allocator& allocator)
: G4VAccumulable(rhs),
fVector(rhs, allocator),
fMergeFunction(rhs.fMergeFunction)
{}
// Move ctor
//_____________________________________________________________________________
template <class T, class Allocator>
G4AccVector<T, Allocator>::G4AccVector(
G4AccVector&& rhs, const Allocator& allocator)
: G4VAccumulable(std::move(rhs)),
fVector(std::move(rhs), allocator),
fMergeFunction(rhs.fMergeFunction)
{}
//_____________________________________________________________________________
template <class T, class Allocator>
void G4AccVector<T, Allocator>::Merge(const G4VAccumulable& other)
{
const auto& otherVector = static_cast<const G4AccVector<T, Allocator>&>(other);
if (G4Accumulables::VerboseLevel > 2 ) {
G4cout << "G4AccVector<T, Allocator>::Merge: " << G4endl;
G4cout << "destination: ";
for (const auto& v : fVector) {
G4cout << v << ", ";
}
G4cout << G4endl;
G4cout << "merged data: ";
for (const auto& v : otherVector.fVector) {
G4cout << v << ", ";
}
G4cout << G4endl;
}
std::transform(fVector.begin(), fVector.end(), otherVector.fVector.begin(),
fVector.begin(), fMergeFunction);
}
//_____________________________________________________________________________
template <class T, class Allocator>
void G4AccVector<T, Allocator>::Reset()
{
for (auto& value : fVector) {
value = fInitValue;
}
}
//_____________________________________________________________________________
template <class T, class Allocator>
void G4AccVector<T, Allocator>::Print(G4PrintOptions options) const
{
if (options.Has(G4PrintOptions::kType)) {
G4cout << "vector<" << typeid(T).name() << ">: ";
}
PrintBase(options);
bool first = true;
for (const auto& value : fVector) {
if (! first) { G4cout << ", "; }
G4cout << value;
first = false;
}
G4cout << G4endl;
}
//_____________________________________________________________________________
template <class T, class Allocator>
void G4AccVector<T, Allocator>::SetMergeMode(G4MergeMode value)
{
G4VAccumulable::SetMergeMode(value);
fMergeFunction = GetMergeFunction<T>(fMergeMode);
}
@@ -24,60 +24,14 @@
// ********************************************************************
//
// Class template for accumulables handled by Geant4 analysis
//
// Author: Ivana Hrivnacova, 07/09/2015 (ivana@ipno.in2p3.fr)
// Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 06/08/2024
#ifndef G4Accumulable_h
#define G4Accumulable_h 1
#include "G4VAccumulable.hh"
#include "G4MergeMode.hh"
#include "G4AccValue.hh"
#include "globals.hh"
template <typename T>
class G4Accumulable : public G4VAccumulable
{
public:
G4Accumulable(const G4String& name, T initValue,
G4MergeMode mergeMode = G4MergeMode::kAddition);
G4Accumulable(T initValue = 0,
G4MergeMode mergeMode = G4MergeMode::kAddition);
G4Accumulable(const G4Accumulable& rhs);
G4Accumulable(G4Accumulable&& rhs) noexcept;
~G4Accumulable() override = default;
// Operators
G4Accumulable<T>& operator= (const G4Accumulable<T>& rhs);
G4Accumulable<T>& operator=(G4Accumulable<T>&& rhs) noexcept;
G4Accumulable<T>& operator+=(const G4Accumulable<T>& rhs);
G4Accumulable<T>& operator*=(const G4Accumulable<T>& rhs);
G4Accumulable<T> operator++(int); // postfix increment
G4Accumulable<T>& operator++(); // prefix increment
G4Accumulable<T>& operator= (const T& rhs);
G4Accumulable<T>& operator+=(const T& rhs);
G4Accumulable<T>& operator*=(const T& rhs);
// Methods
void Merge(const G4VAccumulable& other) final;
void Reset() final;
// Get methods
T GetValue() const;
G4MergeMode GetMergeMode() const;
private:
// Data members
T fValue;
T fInitValue;
G4MergeMode fMergeMode;
G4MergeFunction<T> fMergeFunction;
};
// inline functions
#include "G4Accumulable.icc"
template<typename T>
using G4Accumulable = G4AccValue<T>;
#endif
@@ -26,22 +26,30 @@
// The common implementation of analysis manager classes.
// Author: Ivana Hrivnacova, 07/09/2015 (ivana@ipno.in2p3.fr)
// Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 07/09/2015
#ifndef G4AccumulableManager_h
#define G4AccumulableManager_h 1
#include "G4Accumulable.hh"
#include "G4AccValue.hh"
#include "G4AccType.hh"
#include "globals.hh"
#include <map>
#include <vector>
class G4AccumulableManager;
class G4AnalysisManagerState;
class G4VAccumulable;
template <class T>
class G4ThreadLocalSingleton;
template <class T, std::size_t N>
class G4AccArray;
template <class Key, class T, class Compare, class Allocator>
class G4AccMap;
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
class G4AccUnorderedMap;
template <class T, class Allocator>
class G4AccVector;
class G4AccumulableManager
{
@@ -58,38 +66,124 @@ class G4AccumulableManager
// Create accumulables
//
template <typename T>
G4Accumulable<T>*
G4AccValue<T>*
CreateAccValue(const G4String& name, T value,
G4MergeMode mergeMode = G4MergeMode::kAddition);
template <typename T>
G4AccValue<T>*
CreateAccValue(T value,
G4MergeMode mergeMode = G4MergeMode::kAddition);
// Deprecated function using the old accumulable value name
//
template <typename T>
[[deprecated("Use `G4AccumulableManager::CreateAccValue<T>` instead")]]
G4AccValue<T>*
CreateAccumulable(const G4String& name, T value,
G4MergeMode mergeMode = G4MergeMode::kAddition);
template <typename T>
G4Accumulable<T>*
[[deprecated("Use `G4AccumulableManager::CreateAccValue<T>` instead")]]
G4AccValue<T>*
CreateAccumulable(T value,
G4MergeMode mergeMode = G4MergeMode::kAddition);
// Register existing accumulables
//
// templated accumulable
template <typename T>
G4bool RegisterAccumulable(G4Accumulable<T>& accumulable);
G4bool Register(G4AccValue<T>& accumulable);
template <class T, std::size_t N>
G4bool Register(G4AccArray<T, N>& accumulableArray);
template <class Key, class T, class Compare, class Allocator>
G4bool Register(G4AccMap<Key, T, Compare, Allocator>& accumulableMap);
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
G4bool Register(G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>& accumulableUnorderedMap);
template <class T, class Allocator>
G4bool Register(G4AccVector<T, Allocator>& accumulableVector);
// user defined accumulable
G4bool Register(G4VAccumulable* accumulable);
// Deprecated functions with long name
//
template <typename T>
[[deprecated("Use `G4AccumulableManager::Register` instead")]]
G4bool RegisterAccumulable(G4AccValue<T>& accumulable);
// user defined accumulable
[[deprecated("Use `G4AccumulableManager::Register` instead")]]
G4bool RegisterAccumulable(G4VAccumulable* accumulable);
// Access registered accumulables
//
// Via name
// templated accumulable
//
template <typename T>
G4Accumulable<T>* GetAccumulable(const G4String& name, G4bool warn = true) const;
G4AccValue<T>* GetAccValue(const G4String& name, G4bool warn = true) const;
template <class T, std::size_t N>
G4AccArray<T, N>*
GetAccArray(const G4String& name, G4bool warn = true) const;
template <class Key, class T, class Compare = std::less<Key>,
class Allocator = std::allocator<std::pair<const Key, T>>>
G4AccMap<Key, T, Compare, Allocator>*
GetAccMap(const G4String& name, G4bool warn = true) const;
template <class Key, class T, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>,
class Allocator = std::allocator<std::pair<const Key, T>>>
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>*
GetAccUnorderedMap(const G4String& name, G4bool warn = true) const;
template <class T, class Allocator = std::allocator<T>>
G4AccVector<T, Allocator>*
GetAccVector(const G4String& name, G4bool warn = true) const;
// user defined accumulable
G4VAccumulable* GetAccumulable(const G4String& name, G4bool warn = true) const;
// Via id (in the order of registering)
// templated accumulable
// Deprecated function using the old accumulable value name
template <typename T>
G4Accumulable<T>* GetAccumulable(G4int id, G4bool warn = true) const;
[[deprecated("Use `G4AccumulableManager::GetAccValue<T>` instead")]]
G4AccValue<T>* GetAccumulable(const G4String& name, G4bool warn = true) const;
// Via id (in the order of registering)
// templated accumulable
//
template <typename T>
G4AccValue<T>* GetAccValue(G4int id, G4bool warn = true) const;
template <class T, std::size_t N>
G4AccArray<T, N>* GetAccArray(G4int id, G4bool warn = true) const;
template <class Key, class T, class Compare = std::less<Key>,
class Allocator = std::allocator<std::pair<const Key, T>>>
G4AccMap<Key, T, Compare, Allocator>*
GetAccMap(G4int id, G4bool warn = true) const;
template <class Key, class T, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>,
class Allocator = std::allocator<std::pair<const Key, T>>>
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>*
GetAccUnorderedMap(G4int id, G4bool warn = true) const;
template <class T, class Allocator = std::allocator<T>>
G4AccVector<T, Allocator>*
GetAccVector(G4int id, G4bool warn = true) const;
// user defined accumulable
G4VAccumulable* GetAccumulable(G4int id, G4bool warn = true) const;
// Deprecated function using the old accumulable value name
template <typename T>
[[deprecated("Use `G4AccumulableManager::GetAccValue<T>` instead")]]
G4AccValue<T>* GetAccumulable(G4int id, G4bool warn = true) const;
G4int GetNofAccumulables() const;
// Via vector iterators
@@ -101,6 +195,20 @@ class G4AccumulableManager
// Methods applied to all accumulables
void Merge();
void Reset();
void Print(G4PrintOptions options = G4PrintOptions()) const;
// Print a selected range
void Print(G4int startId, G4int count,
G4PrintOptions options = G4PrintOptions()) const;
void Print(std::vector<G4VAccumulable*>::iterator startIt, std::size_t count,
G4PrintOptions options = G4PrintOptions()) const;
void Print(std::vector<G4VAccumulable*>::iterator startIt,
std::vector<G4VAccumulable*>::iterator endIt,
G4PrintOptions options = G4PrintOptions()) const;
// Verbose level
void SetVerboseLevel(G4int value);
G4int GetVerboseLevel() const;
private:
// Hide singleton ctor
@@ -111,9 +219,27 @@ class G4AccumulableManager
G4String GenerateName() const;
// Check if a name is already used in a map and print a warning
G4bool CheckName(const G4String& name, const G4String& where) const;
G4bool CheckType(G4VAccumulable* accumulable, G4AccType type, G4bool warn) const;
template <typename T>
G4Accumulable<T>* GetAccumulable(G4VAccumulable* accumulable, G4bool warn) const;
G4AccValue<T>* GetAccumulable(G4VAccumulable* accumulable, G4bool warn) const;
template <typename T, std::size_t N>
G4AccArray<T, N>* GetAccArray(G4VAccumulable* accumulable, G4bool warn) const;
template <typename Key, typename T, typename Compare = std::less<Key>,
typename Allocator = std::allocator<std::pair<const Key, T>>>
G4AccMap<Key, T, Compare, Allocator>*
GetAccMap(G4VAccumulable* accumulable, G4bool warn = true) const;
template <typename Key, typename T, typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>,
typename Allocator = std::allocator<std::pair<const Key, T>>>
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>*
GetAccUnorderedMap(G4VAccumulable* accumulable, G4bool warn = true) const;
template <typename T, typename Allocator = std::allocator<T>>
G4AccVector<T, Allocator>*
GetAccVector(G4VAccumulable* accumulable, G4bool warn) const;
// Constants
const G4String kBaseName = "accumulable";
@@ -24,42 +24,119 @@
// ********************************************************************
//
// Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 07/09/2015
#include "G4AccArray.hh"
#include "G4AccMap.hh"
#include "G4AccUnorderedMap.hh"
#include "G4AccVector.hh"
//_____________________________________________________________________________
template <typename T>
G4Accumulable<T>*
G4AccValue<T>*
G4AccumulableManager::GetAccumulable(G4VAccumulable* accumulable, G4bool warn) const
{
// Do not check type if nullptr
if ( ! accumulable ) return nullptr;
// check type
auto taccumulable = dynamic_cast<G4Accumulable<T>*>(accumulable);
if ( ! taccumulable && warn ) {
G4ExceptionDescription description;
description << " " << "Accumulable " << accumulable->GetName()
<< " has different type";
G4Exception("G4AccumulableManager::GetAccumulable<T>",
"Analysis_W001", JustWarning, description);
if (accumulable == nullptr) {
return nullptr;
}
return taccumulable;
// check type
if (! CheckType(accumulable, G4AccType::kValue, warn)) {
return nullptr;
}
return static_cast<G4AccValue<T>*>(accumulable);
}
//_____________________________________________________________________________
template <typename T, std::size_t N>
G4AccArray<T, N>*
G4AccumulableManager::GetAccArray(G4VAccumulable* accumulable, G4bool warn) const
{
// Do not check type if nullptr
if (accumulable == nullptr) {
return nullptr;
}
// check type
if (! CheckType(accumulable, G4AccType::kArray, warn)) {
return nullptr;
}
return static_cast<G4AccArray<T, N>*>(accumulable);
}
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>*
G4AccumulableManager::GetAccMap(G4VAccumulable* accumulable, G4bool warn) const
{
// Do not check type if nullptr
if (accumulable == nullptr) {
return nullptr;
}
// check type
if (! CheckType(accumulable, G4AccType::kMap, warn)) {
return nullptr;
}
return static_cast<G4AccMap<Key, T, Compare, Allocator>*>(accumulable);
}
//_____________________________________________________________________________
template <typename Key, typename T, typename Hash, typename KeyEqual, typename Allocator>
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>*
G4AccumulableManager::GetAccUnorderedMap(G4VAccumulable* accumulable, G4bool warn) const
{
// Do not check type if nullptr
if (accumulable == nullptr) {
return nullptr;
}
// check type
if (! CheckType(accumulable, G4AccType::kUnorderedMap, warn)) {
return nullptr;
}
return static_cast<G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>*>(accumulable);
}
//_____________________________________________________________________________
template <class T, class Allocator>
G4AccVector<T, Allocator>*
G4AccumulableManager::GetAccVector(G4VAccumulable* accumulable, G4bool warn) const
{
// Do not check type if nullptr
if (accumulable == nullptr) {
return nullptr;
}
// check type
if (! CheckType(accumulable, G4AccType::kVector, warn)) {
return nullptr;
}
return static_cast<G4AccVector<T, Allocator>*>(accumulable);
}
//_____________________________________________________________________________
template <typename T>
G4Accumulable<T>*
G4AccumulableManager::CreateAccumulable(
G4AccValue<T>*
G4AccumulableManager::CreateAccValue(
const G4String& name, T value, G4MergeMode mergeMode)
{
// do not accept name if it is already used
if ( ! CheckName(name, "CreateAccumulable") ) return 0;
if (!CheckName(name, "CreateAccumulable")) {
return 0;
}
// create accumulable
auto accumulable = new G4Accumulable<T>(name, value, mergeMode);
auto accumulable = new G4AccValue<T>(name, value, mergeMode);
// register accumulable in the map and vector
RegisterAccumulable(accumulable);
Register(accumulable);
fAccumulablesToDelete.push_back(accumulable);
return accumulable;
@@ -67,40 +144,196 @@ G4AccumulableManager::CreateAccumulable(
//_____________________________________________________________________________
template <typename T>
G4Accumulable<T>*
G4AccValue<T>*
G4AccumulableManager::CreateAccValue(
T value, G4MergeMode mergeMode)
{
return CreateAccValue<T>(G4String(), value, mergeMode);
}
// Deprecated function using the old accumulable value name
//_____________________________________________________________________________
template <typename T>
G4AccValue<T>*
G4AccumulableManager::CreateAccumulable(
const G4String& name, T value, G4MergeMode mergeMode)
{
return CreateAccValue<T>(name, value, mergeMode);
}
// Deprecated function using the old accumulable value name
//_____________________________________________________________________________
template <typename T>
G4AccValue<T>*
G4AccumulableManager::CreateAccumulable(
T value, G4MergeMode mergeMode)
{
return CreateAccumulable(G4String(), value, mergeMode);
return CreateAccValue<T>(value, mergeMode);
}
//_____________________________________________________________________________
template <typename T>
G4bool G4AccumulableManager::RegisterAccumulable(G4Accumulable<T>& accumulable)
G4bool G4AccumulableManager::Register(G4AccValue<T>& accumulable)
{
return RegisterAccumulable(&accumulable);
return Register(&accumulable);
}
//_____________________________________________________________________________
template <class T, std::size_t N>
G4bool G4AccumulableManager::Register(G4AccArray<T, N>& accumulableArray)
{
return Register(&accumulableArray);
}
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4bool G4AccumulableManager::Register(
G4AccMap<Key, T, Compare, Allocator>& accumulableMap)
{
return Register(&accumulableMap);
}
//_____________________________________________________________________________
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
G4bool G4AccumulableManager::Register(
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>& accumulableUnorderedMap)
{
return Register(&accumulableUnorderedMap);
}
//_____________________________________________________________________________
template <class T, class Allocator>
G4bool G4AccumulableManager::Register(G4AccVector<T, Allocator>& accumulableVector)
{
return Register(&accumulableVector);
}
// Deprecated functions with long name
//_____________________________________________________________________________
template <typename T>
G4bool G4AccumulableManager::RegisterAccumulable(G4AccValue<T>& accumulable)
{
return Register(&accumulable);
}
//_____________________________________________________________________________
template <typename T>
G4Accumulable<T>*
G4AccumulableManager::GetAccumulable(const G4String& name, G4bool warn) const
G4AccValue<T>*
G4AccumulableManager::GetAccValue(const G4String& name, G4bool warn) const
{
// get G4VParammeter from the map
// get G4VAccummulable from the map
auto accumulable = GetAccumulable(name, warn);
return GetAccumulable<T>(accumulable, warn);
}
//_____________________________________________________________________________
template <typename T>
G4Accumulable<T>*
G4AccumulableManager::GetAccumulable(G4int id, G4bool warn) const
template <class T, std::size_t N>
G4AccArray<T, N>*
G4AccumulableManager::GetAccArray(const G4String& name, G4bool warn) const
{
// get G4VParammeter from the map
// get G4VAccummulable from the map
auto accumulable = GetAccumulable(name, warn);
return GetAccArray<T,N>(accumulable, warn);
}
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>*
G4AccumulableManager::GetAccMap(const G4String& name, G4bool warn) const
{
// get G4VAccummulable from the map
auto accumulable = GetAccumulable(name, warn);
return GetAccMap<Key, T, Compare, Allocator>(accumulable, warn);
}
//_____________________________________________________________________________
template <typename Key, typename T, typename Hash, typename KeyEqual, typename Allocator>
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>*
G4AccumulableManager::GetAccUnorderedMap(const G4String& name, G4bool warn) const
{
// get G4VAccummulable from the map
auto accumulable = GetAccumulable(name, warn);
return GetAccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>(accumulable, warn);
}
//_____________________________________________________________________________
template <class T, class Allocator>
G4AccVector<T, Allocator>*
G4AccumulableManager::GetAccVector(const G4String& name, G4bool warn) const
{
// get G4VAccummulable from the map
auto accumulable = GetAccumulable(name, warn);
return GetAccVector<T,Allocator>(accumulable, warn);
}
// Deprecated function using the old accumulable value name
//_____________________________________________________________________________
template <typename T>
G4AccValue<T>*
G4AccumulableManager::GetAccumulable(const G4String& name, G4bool warn) const
{
return GetAccValue<T>(name, warn);
}
//_____________________________________________________________________________
template <typename T>
G4AccValue<T>*
G4AccumulableManager::GetAccValue(G4int id, G4bool warn) const
{
// get G4VAccummulable from the map
auto accumulable = GetAccumulable(id, warn);
return GetAccumulable<T>(accumulable, warn);
}
//_____________________________________________________________________________
template <class T, std::size_t N>
G4AccArray<T, N>*
G4AccumulableManager::GetAccArray(G4int id, G4bool warn) const
{
// get G4VAccummulable from the map
auto accumulable = GetAccumulable(id, warn);
return GetAccArray<T,N>(accumulable, warn);
}
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>*
G4AccumulableManager::GetAccMap(G4int id, G4bool warn) const
{
// get G4VAccummulable from the map
auto accumulable = GetAccumulable(id, warn);
return GetAccMap<Key, T, Compare, Allocator>(accumulable, warn);
}
//_____________________________________________________________________________
template <typename Key, typename T, typename Hash, typename KeyEqual, typename Allocator>
G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>*
G4AccumulableManager::GetAccUnorderedMap(G4int id, G4bool warn) const
{
// get G4VAccummulable from the map
auto accumulable = GetAccumulable(id, warn);
return GetAccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>(accumulable, warn);
}
//_____________________________________________________________________________
template <class T, class Allocator>
G4AccVector<T, Allocator>*
G4AccumulableManager::GetAccVector(G4int id, G4bool warn) const
{
// get G4VAccummulable from the map
auto accumulable = GetAccumulable(id, warn);
return GetAccVector<T,Allocator>(accumulable, warn);
}
// Deprecated function using the old accumulable value name
//_____________________________________________________________________________
template <typename T>
G4AccValue<T>*
G4AccumulableManager::GetAccumulable(G4int id, G4bool warn) const
{
return GetAccValue<T>(id, warn);
}
//_____________________________________________________________________________
inline G4int G4AccumulableManager::GetNofAccumulables() const
{
@@ -132,3 +365,15 @@ G4AccumulableManager::EndConst() const
{
return fVector.end();
}
//_____________________________________________________________________________
inline void G4AccumulableManager::SetVerboseLevel(G4int value)
{
G4Accumulables::VerboseLevel = value;
}
//_____________________________________________________________________________
inline G4int G4AccumulableManager::GetVerboseLevel() const
{
return G4Accumulables::VerboseLevel;
}
@@ -24,7 +24,7 @@
// ********************************************************************
//
// Author: Ivana Hrivnacova, 04/07/2012 (ivana@ipno.in2p3.fr)
// Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 04/07/2012
#ifndef G4MergeMode_h
#define G4MergeMode_h 1
@@ -58,18 +58,15 @@ G4MergeFunction<T> GetMergeFunction(G4MergeMode mergeMode)
{
switch ( mergeMode ) {
case G4MergeMode::kAddition:
// return std::bind([](const T& x, const T& y) { return x + y; });
return [](const T& x, const T& y) { return x + y; };
return std::plus<T>();
case G4MergeMode::kMultiplication:
return [](const T& x, const T& y) { return x * y; };
return std::multiplies<T>();
case G4MergeMode::kMaximum:
// return std::bind([](const T& x, const T& y) { return std::max(x,y);});
return [](const T& x, const T& y) { return std::max(x,y); };
case G4MergeMode::kMinimum:
// return std::bind([](const T& x, const T& y) { return std::min(x,y);});
return [](const T& x, const T& y) { return std::min(x,y); };
}
@@ -26,21 +26,22 @@
// Value type independent base class for accumulables handled by Geant4 analysis
//
// Author: Ivana Hrivnacova, 04/09/2015 (ivana@ipno.in2p3.fr)
// Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 04/09/2015
#ifndef G4VAccumulable_h
#define G4VAccumulable_h 1
#include "globals.hh"
#include "G4AccType.hh"
#include "G4MergeMode.hh"
#include "globals.hh"
class G4VAccumulable
{
// To allow G4AccumulableManager set name if not defined by user
friend class G4AccumulableManager;
public:
G4VAccumulable(G4String name = "");
G4VAccumulable(G4MergeMode mergeMode = G4MergeMode::kAddition);
G4VAccumulable(const G4String& name,
G4MergeMode mergeMode = G4MergeMode::kAddition);
G4VAccumulable(const G4VAccumulable& rhs) = default;
G4VAccumulable(G4VAccumulable&& rhs) = default;
virtual ~G4VAccumulable() = default;
@@ -52,12 +53,26 @@ class G4VAccumulable
// Methods
virtual void Merge(const G4VAccumulable& other) = 0;
virtual void Reset() = 0;
virtual void Print(G4PrintOptions options = G4PrintOptions()) const;
virtual void SetMergeMode(G4MergeMode value);
// Set methods
void SetName(const G4String& name);
void SetId(G4int id);
// Get methods
G4String GetName() const;
G4MergeMode GetMergeMode() const;
G4int GetId() const;
virtual G4AccType GetType() const { return G4AccType::kUser; }
protected:
void PrintBase(G4PrintOptions options) const;
G4String fName;
G4MergeMode fMergeMode = G4MergeMode::kAddition;
G4int fId = G4Accumulables::kInvalidId;
};
#include "G4VAccumulable.icc"
@@ -24,15 +24,92 @@
// ********************************************************************
//
// Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 04/09/2015
#include <utility>
//_____________________________________________________________________________
inline G4VAccumulable::G4VAccumulable(G4String name)
: fName(std::move(name))
inline G4VAccumulable::G4VAccumulable(G4MergeMode mergeMode)
: fMergeMode(mergeMode)
{}
//_____________________________________________________________________________
inline G4VAccumulable::G4VAccumulable(const G4String& name, G4MergeMode mergeMode)
: fName(name),
fMergeMode(mergeMode)
{}
//
// protected methods
//
//_____________________________________________________________________________
inline void G4VAccumulable::PrintBase(G4PrintOptions options) const
{
if (options.Has(G4PrintOptions::kName)) {
G4cout << "\"" << GetName() << "\", ";
}
if (options.Has(G4PrintOptions::kId)) {
G4cout << "id: " << fId << ", ";
}
}
//
// public methods
//
//_____________________________________________________________________________
inline void G4VAccumulable::SetName(const G4String& name)
{
if ( fId != G4Accumulables::kInvalidId) {
G4Exception(
"G4VAccumulable::SetName", "Analysis_W001",
JustWarning, "Accumulable was already registered. Setting is ignored");
return;
}
fName = name;
}
//_____________________________________________________________________________
inline void G4VAccumulable::Print(G4PrintOptions options) const
{
PrintBase(options);
}
//_____________________________________________________________________________
inline void G4VAccumulable::SetMergeMode(G4MergeMode value)
{
fMergeMode = value;
}
//_____________________________________________________________________________
inline void G4VAccumulable::SetId(G4int id)
{
if ( fId != G4Accumulables::kInvalidId) {
G4Exception(
"G4VAccumulable::SetId", "Analysis_W001",
JustWarning, "Accumulable was already registered. Setting is ignored");
return;
}
fId = id;
}
//_____________________________________________________________________________
inline G4String G4VAccumulable::GetName() const
{
return fName;
}
//_____________________________________________________________________________
inline G4MergeMode G4VAccumulable::GetMergeMode() const
{
return fMergeMode;
}
//_____________________________________________________________________________
inline G4int G4VAccumulable::GetId() const
{
return fId;
}
+12 -2
View File
@@ -4,10 +4,20 @@
geant4_add_module(G4accumulables
PUBLIC_HEADERS
G4MergeMode.hh
G4Accumulable.hh
G4AccumulableManager.hh
G4AccumulableManager.icc
G4Accumulable.hh
G4Accumulable.icc
G4AccValue.hh
G4AccValue.icc
G4AccArray.hh
G4AccArray.icc
G4AccMap.hh
G4AccMap.icc
G4AccUnorderedMap.hh
G4AccUnorderedMap.icc
G4AccVector.hh
G4AccVector.icc
G4AccType.hh
G4VAccumulable.hh
G4VAccumulable.icc
SOURCES
@@ -24,7 +24,7 @@
// ********************************************************************
//
// Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
// Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 18/06/2013
#include "G4AccumulableManager.hh"
#include "G4ThreadLocalSingleton.hh"
@@ -36,6 +36,15 @@
namespace {
//Mutex to lock master manager when merging accumulables
G4Mutex mergeMutex = G4MUTEX_INITIALIZER;
void RangeException(const G4String& where, const G4String& message)
{
G4ExceptionDescription description;
description << "Invalid range " << message;
G4String method("G4AccumulableManager::");
method.append(where);
G4Exception(where, "Analysis_W001", JustWarning, description);
}
}
//_____________________________________________________________________________
@@ -48,7 +57,9 @@ G4AccumulableManager* G4AccumulableManager::Instance()
//_____________________________________________________________________________
G4AccumulableManager::G4AccumulableManager()
{
if ( ! G4Threading::IsWorkerThread() ) fgMasterInstance = this;
if (!G4Threading::IsWorkerThread()) {
fgMasterInstance = this;
}
}
//_____________________________________________________________________________
@@ -76,9 +87,12 @@ G4String G4AccumulableManager::GenerateName() const
}
//_____________________________________________________________________________
G4bool G4AccumulableManager::CheckName(const G4String& name, const G4String& where) const
G4bool G4AccumulableManager::CheckName(
const G4String& name, const G4String& where) const
{
if ( fMap.find(name) == fMap.end() ) return true;
if (fMap.find(name) == fMap.end()) {
return true;
}
G4ExceptionDescription description;
description << "Name " << name << " is already used." << G4endl;
@@ -89,29 +103,66 @@ G4bool G4AccumulableManager::CheckName(const G4String& name, const G4String& whe
return false;
}
//_____________________________________________________________________________
G4bool G4AccumulableManager::CheckType(
G4VAccumulable* accumulable, G4AccType type, G4bool warn) const
{
if (accumulable->GetType() != type) {
if (warn) {
G4ExceptionDescription description;
description << " " << accumulable->GetName() << ": Incompatible type.";
G4Exception("G4AccumulableManager::CheckType",
"Analysis_W001", JustWarning, description);
}
return false;
}
return true;
}
//
// public methods
//
//_____________________________________________________________________________
G4bool G4AccumulableManager::RegisterAccumulable(G4VAccumulable* accumulable)
G4bool G4AccumulableManager::Register(G4VAccumulable* accumulable)
{
auto name = accumulable->GetName();
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "Going to register accumulable \"" << name << "\"" << G4endl;
}
// do not accept name if it is already used
if ( ! CheckName(name, "RegisterAccumulable") ) return false;
if (!CheckName(name, "RegisterAccumulable")) {
return false;
}
// generate name if empty
if (name.length() == 0u) {
name = GenerateName();
accumulable->fName = name;
accumulable->SetName(name);
}
// set Id
accumulable->SetId((G4int)fVector.size());
fMap[name] = accumulable;
fVector.push_back(accumulable);
if (G4Accumulables::VerboseLevel > 0 ) {
G4cout << "Accumulable registered as \"" << accumulable->GetName() << "\"" << G4endl;
}
return true;
}
// Deprecated functions with long name
//_____________________________________________________________________________
G4bool G4AccumulableManager::RegisterAccumulable(G4VAccumulable* accumulable)
{
return Register(accumulable);
}
//_____________________________________________________________________________
G4VAccumulable*
G4AccumulableManager::GetAccumulable(const G4String& name, G4bool warn) const
@@ -154,7 +205,9 @@ void G4AccumulableManager::Merge()
{
// Do nothing if there are no accumulables registered
// or if master thread
if ((fVector.size() == 0u) || (! G4Threading::IsWorkerThread())) return;
if ((fVector.size() == 0u) || (!G4Threading::IsWorkerThread())) {
return;
}
// The manager on mastter must exist
if (fgMasterInstance == nullptr) {
@@ -186,11 +239,59 @@ void G4AccumulableManager::Merge()
//_____________________________________________________________________________
void G4AccumulableManager::Reset()
{
// Reset histograms and profiles
// Reset all accummulables
for ( auto it : fVector ) {
it->Reset();
}
}
//_____________________________________________________________________________
void G4AccumulableManager::Print(G4PrintOptions options) const
{
for ( auto it : fVector ) {
it->Print(options);
}
}
//_____________________________________________________________________________
void G4AccumulableManager::Print(
G4int startId, G4int count, G4PrintOptions options) const
{
// check if range is within limits
if ( startId < 0 || startId >= G4int(fVector.size()) ||
count <= 0 || startId + count > G4int(fVector.size()) ) {
RangeException("Print",
std::to_string(startId) + ", " + std::to_string(count));
return;
}
for ( auto id = startId; id < startId + count; ++id ) {
fVector[id]->Print(options);
}
}
//_____________________________________________________________________________
void G4AccumulableManager::Print(
std::vector<G4VAccumulable*>::iterator startIt,
std::vector<G4VAccumulable*>::iterator endIt,
G4PrintOptions options) const
{
// check if range is within limits
if ( startIt == fVector.end() || endIt == fVector.end() ) {
RangeException("Print", "[startIt, endIt]");
return;
}
for ( auto it = startIt; it != endIt; ++it ) {
(*it)->Print(options);
}
}
//_____________________________________________________________________________
void G4AccumulableManager::Print(
std::vector<G4VAccumulable*>::iterator startIt, std::size_t count,
G4PrintOptions options) const
{
Print(startIt, startIt+count, options);
}
@@ -24,7 +24,7 @@
// ********************************************************************
//
// Author: Ivana Hrivnacova, 22/08/2013 (ivana@ipno.in2p3.fr)
// Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 22/08/2013
#include "G4MergeMode.hh"
@@ -34,8 +34,12 @@ namespace G4Accumulables
//_____________________________________________________________________________
G4MergeMode GetMergeMode(const G4String& mergeModeName)
{
if (mergeModeName == "+") return G4MergeMode::kAddition;
if (mergeModeName == "*") return G4MergeMode::kMultiplication;
if (mergeModeName == "+") {
return G4MergeMode::kAddition;
}
if (mergeModeName == "*") {
return G4MergeMode::kMultiplication;
}
G4ExceptionDescription description;
description << "\"" << mergeModeName << "\" merge mode is not supported." << G4endl