484 lines
16 KiB
Plaintext
484 lines
16 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->GetNtuple() ) {
|
|
if ( warn ) {
|
|
G4Analysis::Warn("Ntuple " + to_string(id) + " does not exist.",
|
|
fkClass, functionName);
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
return ntupleDescription->GetNtuple();
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
template <typename T>
|
|
G4bool G4TNtupleManager<NT, FT>::FillNtupleTColumn(
|
|
G4int ntupleId, G4int columnId, const T& value)
|
|
{
|
|
if (fNewCycle && fNtupleVector.empty()) {
|
|
CreateNtuplesFromBooking(*fNtupleBookingVector);
|
|
fNewCycle = false;
|
|
}
|
|
|
|
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;
|
|
|
|
// Allocate the vector element(s) if needed
|
|
while ( index >= G4int(fNtupleDescriptionVector.size()) ) {
|
|
fNtupleDescriptionVector.push_back(nullptr);
|
|
}
|
|
|
|
auto ntupleDescription = fNtupleDescriptionVector[index];
|
|
if (ntupleDescription == nullptr) {
|
|
// Create ntuple description from ntuple booking
|
|
// if it does not yet exist
|
|
ntupleDescription = new G4TNtupleDescription<NT, FT>(ntupleBooking);
|
|
fNtupleDescriptionVector[index] = ntupleDescription;
|
|
}
|
|
|
|
// Do not create ntuple if it is inactivated
|
|
if ( fState.GetIsActivation() &&
|
|
( ! ntupleDescription->GetActivation() ) ) return G4Analysis::kInvalidId;
|
|
|
|
// Do not create ntuple if it already exists
|
|
if ( ntupleDescription->GetNtuple() != nullptr ) {
|
|
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.
|
|
|
|
// Save the ntuple booking for new cycle
|
|
fNtupleBookingVector = &ntupleBookings;
|
|
|
|
// Create ntuple descriptions from ntuple booking.
|
|
for ( auto ntupleBooking : ntupleBookings ) {
|
|
CreateNtuple(ntupleBooking);
|
|
}
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
G4bool
|
|
G4TNtupleManager<NT, FT>::Reset()
|
|
{
|
|
// Reset ntuple description, this will delete ntuple if present and
|
|
// we have its ownership.
|
|
// The ntuples will be recreated with new cycle or new open file.
|
|
|
|
for ( auto ntupleDescription : fNtupleDescriptionVector ) {
|
|
ntupleDescription->Reset();
|
|
}
|
|
|
|
fNtupleVector.clear();
|
|
|
|
return true;
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
void
|
|
G4TNtupleManager<NT, FT>::Clear()
|
|
{
|
|
// Clear all data
|
|
|
|
for ( auto ntupleDescription : fNtupleDescriptionVector ) {
|
|
delete ntupleDescription;
|
|
}
|
|
|
|
fNtupleDescriptionVector.clear();
|
|
fNtupleVector.clear();
|
|
|
|
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->GetNtuple();
|
|
if ( ! ntuple ) return false;
|
|
|
|
auto result = ntuple->add_row();
|
|
if ( ! result ) {
|
|
G4Analysis::Warn(
|
|
"Ntuple " + to_string(ntupleId) + " adding row has failed.",
|
|
fkClass, "AddTNtupleRow");
|
|
}
|
|
|
|
ntupleDescription->SetHasFill(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->SetActivation(activation);
|
|
}
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
void G4TNtupleManager<NT, FT>::SetActivation(
|
|
G4int ntupleId, G4bool activation)
|
|
{
|
|
auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetActivation");
|
|
if ( ! ntupleDescription ) return;
|
|
|
|
ntupleDescription->SetActivation(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->GetActivation();
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
void G4TNtupleManager<NT, FT>::SetNewCycle(G4bool value)
|
|
{
|
|
fNewCycle = value;
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
G4bool G4TNtupleManager<NT, FT>::GetNewCycle() const
|
|
{
|
|
return fNewCycle;
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
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->GetNtuple();
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
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());
|
|
}
|
|
|
|
//_____________________________________________________________________________
|
|
template <typename NT, typename FT>
|
|
G4bool G4TNtupleManager<NT, FT>::List(std::ostream& output, G4bool onlyIfActive)
|
|
{
|
|
// Save current output stream formatting
|
|
std::ios_base::fmtflags outputFlags(output.flags() );
|
|
|
|
// Define optimal field widths
|
|
size_t maxNameLength = 0;
|
|
size_t maxTitleLength = 0;
|
|
// size_t maxEntries = 0;
|
|
size_t nofActive = 0;
|
|
for (size_t i = 0; i < fNtupleDescriptionVector.size(); ++i) {
|
|
// auto ntuple = fNtupleVector[i];
|
|
const auto& ntupleBooking = fNtupleDescriptionVector[i]->GetNtupleBooking();
|
|
if (ntupleBooking.name().length() > maxNameLength) {
|
|
maxNameLength = ntupleBooking.name().length();
|
|
}
|
|
if (ntupleBooking.title().length() > maxTitleLength) {
|
|
maxTitleLength = ntupleBooking.title().length();
|
|
}
|
|
// if (ntuple->entries() > maxEntries) {
|
|
// maxEntries = ntuple->entries();
|
|
// }
|
|
if (fNtupleDescriptionVector[i]->GetActivation()) {
|
|
++nofActive;
|
|
}
|
|
}
|
|
size_t maxIdWidth = std::to_string(fNtupleVector.size() + GetFirstId()).length();
|
|
// size_t maxEntriesWidth = std::to_string(maxEntries).length();
|
|
// update string width for adde double quotas
|
|
maxNameLength += 2;
|
|
maxTitleLength += 2;
|
|
|
|
// List general info
|
|
output << "Ntuple: " << nofActive << " active ";
|
|
if (! onlyIfActive) {
|
|
output << " of " << GetNofNtuples() << " defined ";
|
|
}
|
|
output << G4endl;
|
|
|
|
// List objects
|
|
for (size_t i = 0; i < fNtupleDescriptionVector.size(); ++i) {
|
|
// auto ntuple = fNtupleVector[i];
|
|
const auto& ntupleBooking = fNtupleDescriptionVector[i]->GetNtupleBooking();
|
|
|
|
// skip inactivated objcets
|
|
if (fState.GetIsActivation() && onlyIfActive && (! fNtupleDescriptionVector[i]->GetActivation())) continue;
|
|
|
|
// print selected info
|
|
output << " id: " << std::setw((G4int)maxIdWidth) << GetFirstId() + i
|
|
<< " name: \"" << std::setw((G4int)maxNameLength) << std::left << ntupleBooking.name() + "\""
|
|
<< " title: \"" << std::setw((G4int)maxTitleLength) << std::left << ntupleBooking.title() + "\"";
|
|
// << " entries: " << std::setw((G4int)maxEntriesWidth) << ntuple->entries();
|
|
if (! onlyIfActive) {
|
|
output << " active: " << std::boolalpha << fNtupleDescriptionVector[i]->GetActivation();
|
|
}
|
|
output << G4endl;
|
|
}
|
|
|
|
// Restore the output stream formatting
|
|
output.flags(outputFlags);
|
|
|
|
return output.good();
|
|
}
|