394 lines
12 KiB
Plaintext
394 lines
12 KiB
Plaintext
//
|
|
// ********************************************************************
|
|
// * 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. *
|
|
// ********************************************************************
|
|
//
|
|
|
|
#include "G4AnalysisManagerState.hh"
|
|
#include "G4AnalysisUtilities.hh"
|
|
|
|
using std::to_string;
|
|
|
|
//
|
|
// private template functions
|
|
//
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
G4TNtupleManager<NT, FT>::G4TNtupleManager(
|
|
const G4AnalysisManagerState& state)
|
|
: G4BaseNtupleManager(state)
|
|
{}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
G4TNtupleManager<NT, FT>::~G4TNtupleManager()
|
|
{
|
|
for ( auto ntupleDescription : fNtupleDescriptionVector ) {
|
|
delete ntupleDescription;
|
|
}
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
G4TNtupleDescription<NT, FT>*
|
|
G4TNtupleManager<NT, FT>::GetNtupleDescriptionInFunction(
|
|
G4int id, std::string_view functionName, G4bool warn) const
|
|
{
|
|
auto index = id - fFirstId;
|
|
if ( index < 0 || index >= G4int(fNtupleDescriptionVector.size()) ) {
|
|
if ( warn) {
|
|
G4Analysis::Warn("Ntuple " + to_string(id) + " does not exist.",
|
|
fkClass, functionName);
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
return fNtupleDescriptionVector[index];
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
NT* G4TNtupleManager<NT, FT>::GetNtupleInFunction(
|
|
G4int id, std::string_view functionName, G4bool warn) const
|
|
{
|
|
auto ntupleDescription = GetNtupleDescriptionInFunction(id, functionName);
|
|
if ( ! ntupleDescription ) return nullptr;
|
|
|
|
if ( ! ntupleDescription->fNtuple ) {
|
|
if ( warn ) {
|
|
G4Analysis::Warn("Ntuple " + to_string(id) + " does not exist.",
|
|
fkClass, functionName);
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
return ntupleDescription->fNtuple;
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
template <typename T>
|
|
G4bool G4TNtupleManager<NT, FT>::FillNtupleTColumn(
|
|
G4int ntupleId, G4int columnId, const T& value)
|
|
{
|
|
if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
|
|
//G4cout << "Skipping FillNtupleIColumn for " << ntupleId << G4endl;
|
|
return false;
|
|
}
|
|
|
|
// get ntuple
|
|
auto ntuple = GetNtupleInFunction(ntupleId, "FillNtupleTColumn");
|
|
if ( ! ntuple ) return false;
|
|
|
|
// get generic column
|
|
auto index = columnId - fFirstNtupleColumnId;
|
|
if ( index < 0 || index >= G4int(ntuple->columns().size()) ) {
|
|
G4Analysis::Warn(
|
|
"Ntuple " + to_string(ntupleId) + " column " + to_string(columnId) +
|
|
" does not exist.",
|
|
fkClass, "FillNtupleTColumn");
|
|
return false;
|
|
}
|
|
auto icolumn = ntuple->columns()[index];
|
|
|
|
// get column and check its type
|
|
auto column = dynamic_cast<typename NT::template column<T>* >(icolumn);
|
|
if ( ! column ) {
|
|
G4Analysis::Warn(
|
|
"Column type does not match: "
|
|
" ntuple " + to_string(ntupleId) + " column " + to_string(columnId) +
|
|
" value " + G4Analysis::ToString(value),
|
|
fkClass, "FillNtupleTColumn");
|
|
return false;
|
|
}
|
|
|
|
column->fill(value);
|
|
|
|
if ( IsVerbose(G4Analysis::kVL4) ) {
|
|
Message(G4Analysis::kVL4, "fill", "ntuple T column",
|
|
" ntupleId " + to_string(ntupleId) +
|
|
" column " + to_string(columnId) +
|
|
" value " + G4Analysis::ToString<T>(value));
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
//
|
|
// protected functions
|
|
//
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
G4int G4TNtupleManager<NT, FT>::CreateNtuple(G4NtupleBooking* ntupleBooking)
|
|
{
|
|
Message(G4Analysis::kVL4, "create from booking", "ntuple",
|
|
ntupleBooking->fNtupleBooking.name());
|
|
|
|
// The ntuple index
|
|
auto index = ntupleBooking->fNtupleId - fFirstId;
|
|
|
|
// Check if the ntuple description of this id already exists
|
|
// (what should normally never happen)
|
|
// delete it and print a warning
|
|
if ( index < G4int(fNtupleDescriptionVector.size()) &&
|
|
fNtupleDescriptionVector[index] ) {
|
|
delete fNtupleDescriptionVector[index];
|
|
G4Analysis::Warn(
|
|
"Ntuple description " + to_string(ntupleBooking->fNtupleId) +
|
|
" already exists.", fkClass, "CreateNtuple");
|
|
}
|
|
|
|
// Allocate the vector element(s)
|
|
while ( index >= G4int(fNtupleDescriptionVector.size()) ) {
|
|
fNtupleDescriptionVector.push_back(nullptr);
|
|
}
|
|
|
|
// Create ntuple description from ntuple booking.
|
|
auto ntupleDescription = new G4TNtupleDescription<NT, FT>(ntupleBooking);
|
|
fNtupleDescriptionVector[index] = ntupleDescription;
|
|
|
|
// Do not create ntuple if it is inactivated
|
|
if ( fState.GetIsActivation() &&
|
|
( ! ntupleDescription->fActivation ) ) return G4Analysis::kInvalidId;
|
|
|
|
// Do not create ntuple if it already exists
|
|
if ( ntupleDescription->fNtuple ) {
|
|
G4Analysis::Warn(
|
|
"Ntuple " + to_string(ntupleBooking->fNtupleId) +
|
|
" already exists.", fkClass, "CreateNtuple");
|
|
return ntupleBooking->fNtupleId;
|
|
}
|
|
|
|
// create ntuple
|
|
CreateTNtupleFromBooking(ntupleDescription);
|
|
|
|
// finish created ntuple
|
|
auto fromBooking = true;
|
|
FinishTNtuple(ntupleDescription, fromBooking);
|
|
|
|
Message(G4Analysis::kVL3, "create from booking", "ntuple",
|
|
ntupleBooking->fNtupleBooking.name());
|
|
|
|
return ntupleBooking->fNtupleId;
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
void G4TNtupleManager<NT, FT>::CreateNtuplesFromBooking(
|
|
const std::vector<G4NtupleBooking*>& ntupleBookings)
|
|
{
|
|
// Create ntuple from ntuple bookings.
|
|
|
|
// Create ntuple descriptions from ntuple booking.
|
|
for ( auto ntupleBooking : ntupleBookings ) {
|
|
CreateNtuple(ntupleBooking);
|
|
}
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
G4bool
|
|
G4TNtupleManager<NT, FT>::Reset()
|
|
{
|
|
for ( auto ntupleDescription : fNtupleDescriptionVector ) {
|
|
delete ntupleDescription;
|
|
}
|
|
|
|
fNtupleDescriptionVector.clear();
|
|
fNtupleVector.clear();
|
|
|
|
return true;
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
void
|
|
G4TNtupleManager<NT, FT>::Clear()
|
|
{
|
|
// Reset function clears all data
|
|
Reset();
|
|
|
|
Message(G4Analysis::kVL2, "clear", "ntuples");
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
G4bool G4TNtupleManager<NT, FT>::FillNtupleIColumn(
|
|
G4int ntupleId, G4int columnId, G4int value)
|
|
{
|
|
return FillNtupleTColumn<int>(ntupleId, columnId, value);
|
|
}
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
G4bool G4TNtupleManager<NT, FT>::FillNtupleFColumn(
|
|
G4int ntupleId, G4int columnId, G4float value)
|
|
{
|
|
return FillNtupleTColumn<float>(ntupleId, columnId, value);
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
G4bool G4TNtupleManager<NT, FT>::FillNtupleDColumn(
|
|
G4int ntupleId, G4int columnId, G4double value)
|
|
{
|
|
return FillNtupleTColumn<double>(ntupleId, columnId, value);
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
G4bool G4TNtupleManager<NT, FT>::FillNtupleSColumn(
|
|
G4int ntupleId, G4int columnId, const G4String& value)
|
|
{
|
|
return FillNtupleTColumn<std::string>(ntupleId, columnId, value);
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
G4bool G4TNtupleManager<NT, FT>::AddNtupleRow(
|
|
G4int ntupleId)
|
|
{
|
|
if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
|
|
//G4cout << "Skipping AddNtupleRow for " << ntupleId << G4endl;
|
|
return false;
|
|
}
|
|
|
|
if ( IsVerbose(G4Analysis::kVL4) ) {
|
|
Message(G4Analysis::kVL4, "add", "ntuple row",
|
|
" ntupleId " + to_string(ntupleId));
|
|
}
|
|
|
|
auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "AddNtupleRow");
|
|
if ( ! ntupleDescription ) return false;
|
|
|
|
auto ntuple = ntupleDescription->fNtuple;
|
|
if ( ! ntuple ) return false;
|
|
|
|
auto result = ntuple->add_row();
|
|
if ( ! result ) {
|
|
G4Analysis::Warn(
|
|
"Ntuple " + to_string(ntupleId) + " adding row has failed.",
|
|
fkClass, "AddTNtupleRow");
|
|
}
|
|
|
|
ntupleDescription->fHasFill = true;
|
|
|
|
if ( IsVerbose(G4Analysis::kVL4) ) {
|
|
Message(G4Analysis::kVL4, "add", "ntuple row",
|
|
" ntupleId " + to_string(ntupleId));
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
void G4TNtupleManager<NT, FT>::SetActivation(
|
|
G4bool activation)
|
|
{
|
|
for ( auto ntupleDescription : fNtupleDescriptionVector ) {
|
|
ntupleDescription->fActivation = activation;
|
|
}
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
void G4TNtupleManager<NT, FT>::SetActivation(
|
|
G4int ntupleId, G4bool activation)
|
|
{
|
|
auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetActivation");
|
|
if ( ! ntupleDescription ) return;
|
|
|
|
ntupleDescription->fActivation = activation;
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
G4bool G4TNtupleManager<NT, FT>::GetActivation(
|
|
G4int ntupleId) const
|
|
{
|
|
auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetActivation");
|
|
if ( ! ntupleDescription ) return false;
|
|
|
|
return ntupleDescription->fActivation;
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
NT*
|
|
G4TNtupleManager<NT, FT>::GetNtuple() const
|
|
{
|
|
return GetNtuple(fFirstId);
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
NT*
|
|
G4TNtupleManager<NT, FT>::GetNtuple(G4int ntupleId) const
|
|
{
|
|
auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetNtuple");
|
|
if ( ! ntupleDescription ) return nullptr;
|
|
|
|
return ntupleDescription->fNtuple;
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
typename std::vector<NT*>::iterator
|
|
G4TNtupleManager<NT, FT>::BeginNtuple()
|
|
{
|
|
return fNtupleVector.begin();
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
typename std::vector<NT*>::iterator
|
|
G4TNtupleManager<NT, FT>::EndNtuple()
|
|
{
|
|
return fNtupleVector.end();
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
typename std::vector<NT*>::const_iterator
|
|
G4TNtupleManager<NT, FT>::BeginConstNtuple() const
|
|
{
|
|
return fNtupleVector.begin();
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
typename std::vector<NT*>::const_iterator
|
|
G4TNtupleManager<NT, FT>::EndConstNtuple() const
|
|
{
|
|
return fNtupleVector.end();
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
G4int G4TNtupleManager<NT, FT>::GetNofNtuples() const
|
|
{
|
|
return G4int(fNtupleVector.size());
|
|
}
|