Import Geant4 11.1.0 source tree

This commit is contained in:
Gabriele Cosmo
2022-12-09 14:43:28 +01:00
parent c07cea1fe0
commit 9f34590941
3810 changed files with 200490 additions and 182326 deletions
@@ -1,131 +0,0 @@
//
// ********************************************************************
// * 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, 24/07/2014 (ivana@ipno.in2p3.fr)
#include "G4BaseHistoUtilities.hh"
#include "G4AnalysisUtilities.hh"
#include "tools/histo/axis"
using std::to_string;
namespace G4Analysis
{
//_____________________________________________________________________________
G4int GetNbins(const G4ToolsBaseHisto& baseHisto, G4int dimension)
{
return baseHisto.get_axis(dimension).bins();
}
//_____________________________________________________________________________
G4double GetMin(const G4ToolsBaseHisto& baseHisto, G4int dimension)
{
// Returns min data value
return baseHisto.get_axis(dimension).lower_edge();
}
//_____________________________________________________________________________
G4double GetMax(const G4ToolsBaseHisto& baseHisto, G4int dimension)
{
// Returns max data value
return baseHisto.get_axis(dimension).upper_edge();
}
//_____________________________________________________________________________
G4double GetWidth(const G4ToolsBaseHisto& baseHisto, G4int dimension,
const G4String& hnType)
{
auto nbins = baseHisto.get_axis(dimension).bins();
if ( ! nbins ) {
Warn("nbins = 0 ! for " + hnType, kNamespaceName, "GetWidth");
return 0.;
}
return ( baseHisto.get_axis(dimension).upper_edge()
- baseHisto.get_axis(dimension).lower_edge() )/nbins;
}
//_____________________________________________________________________________
G4bool SetTitle(G4ToolsBaseHisto& baseHisto, const G4String& title)
{
return baseHisto.set_title(title);
}
//_____________________________________________________________________________
G4bool SetAxisTitle(G4ToolsBaseHisto& baseHisto, G4int dimension,
const G4String& title)
{
if ( dimension == kX ) {
baseHisto.add_annotation(tools::histo::key_axis_x_title(), title);
}
else if ( dimension == kY ) {
baseHisto.add_annotation(tools::histo::key_axis_y_title(), title);
}
else if ( dimension == kZ ) {
baseHisto.add_annotation(tools::histo::key_axis_z_title(), title);
}
return true;
}
//_____________________________________________________________________________
G4String GetTitle(const G4ToolsBaseHisto& baseHisto)
{
return baseHisto.title();
}
//_____________________________________________________________________________
G4String GetAxisTitle(const G4ToolsBaseHisto& baseHisto, G4int dimension,
const G4String& hnType)
{
G4String title;
G4bool result = false;
if ( dimension == kX ) {
result = baseHisto.annotation(tools::histo::key_axis_x_title(), title);
}
else if ( dimension == kY ) {
result = baseHisto.annotation(tools::histo::key_axis_y_title(), title);
}
else if ( dimension == kZ ) {
result = baseHisto.annotation(tools::histo::key_axis_z_title(), title);
}
if ( ! result ) {
Warn("Got wrong dimension " + to_string(dimension) + " for " + hnType,
kNamespaceName, "GetAxisTitle");
return "";
}
return title;
}
}
@@ -1,502 +0,0 @@
//
// ********************************************************************
// * 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, 18/06/2013 (ivana@ipno.in2p3.fr)
#include "G4H1ToolsManager.hh"
#include "G4AnalysisManagerState.hh"
#include "G4BaseHistoUtilities.hh"
#include "G4AnalysisUtilities.hh"
#include "tools/histo/h1d"
#include <fstream>
using namespace G4Analysis;
using std::to_string;
//
// Constructors, destructor
//
//_____________________________________________________________________________
G4H1ToolsManager::G4H1ToolsManager(const G4AnalysisManagerState& state)
: G4VH1Manager(),
G4THnManager<tools::histo::h1d>(state, "H1")
{}
//
// Utility functions
//
namespace {
//_____________________________________________________________________________
void UpdateH1Information(G4HnInformation* hnInformation,
const G4String& unitName,
const G4String& fcnName,
G4BinScheme binScheme)
{
hnInformation->SetDimension(kX, unitName, fcnName, binScheme);
}
//_____________________________________________________________________________
void AddH1Annotation(tools::histo::h1d* h1d,
const G4String& unitName,
const G4String& fcnName)
{
G4String axisTitle;
UpdateTitle(axisTitle, unitName, fcnName);
h1d->add_annotation(tools::histo::key_axis_x_title(), axisTitle);
}
//_____________________________________________________________________________
tools::histo::h1d* CreateToolsH1(const G4String& title,
G4int nbins, G4double xmin, G4double xmax,
const G4String& unitName,
const G4String& fcnName,
const G4String& binSchemeName,
std::string_view className)
{
auto unit = GetUnitValue(unitName);
auto fcn = GetFunction(fcnName);
auto binScheme = GetBinScheme(binSchemeName);
if ( binScheme != G4BinScheme::kLog ) {
if ( binScheme == G4BinScheme::kUser ) {
// This should never happen, but let's make sure about it
// by issuing a warning
Warn("User binning scheme setting was ignored.\n"
"Linear binning will be applied with given (nbins, xmin, xmax) values.",
className, "CreateToolsH1");
}
return new tools::histo::h1d(title, nbins, fcn(xmin/unit), fcn(xmax/unit));
}
else {
// Compute edges
std::vector<G4double> edges;
ComputeEdges(nbins, xmin, xmax, unit, fcn, binScheme, edges);
return new tools::histo::h1d(title, edges);
}
}
//_____________________________________________________________________________
tools::histo::h1d* CreateToolsH1(const G4String& title,
const std::vector<G4double>& edges,
const G4String& unitName,
const G4String& fcnName)
{
auto unit = GetUnitValue(unitName);
auto fcn = GetFunction(fcnName);
// Apply function
std::vector<G4double> newEdges;
ComputeEdges(edges, unit, fcn, newEdges);
return new tools::histo::h1d(title, newEdges);
}
//_____________________________________________________________________________
void ConfigureToolsH1(tools::histo::h1d* h1d,
G4int nbins, G4double xmin, G4double xmax,
const G4String& unitName,
const G4String& fcnName,
const G4String& binSchemeName,
std::string_view className)
{
auto unit = GetUnitValue(unitName);
auto fcn = GetFunction(fcnName);
auto binScheme = GetBinScheme(binSchemeName);
if ( binScheme != G4BinScheme::kLog ) {
if ( binScheme == G4BinScheme::kUser ) {
// This should never happen, but let's make sure about it
// by issuing a warning
Warn("User binning scheme setting was ignored.\n"
"Linear binning will be applied with given (nbins, xmin, xmax) values.",
className, "ConfigureToolsH1");
}
h1d->configure(nbins, fcn(xmin/unit), fcn(xmax/unit));
}
else {
// Compute bins
std::vector<G4double> edges;
ComputeEdges(nbins, xmin, xmax, unit, fcn, binScheme, edges);
h1d->configure(edges);
}
}
//_____________________________________________________________________________
void ConfigureToolsH1(tools::histo::h1d* h1d,
const std::vector<G4double>& edges,
const G4String& unitName,
const G4String& fcnName)
{
// Apply function to edges
auto unit = GetUnitValue(unitName);
auto fcn = GetFunction(fcnName);
std::vector<G4double> newEdges;
ComputeEdges(edges, unit, fcn, newEdges);
h1d->configure(newEdges);
}
}
//
// private methods
//
//_____________________________________________________________________________
void G4H1ToolsManager::AddH1Information(const G4String& name,
const G4String& unitName,
const G4String& fcnName,
G4BinScheme binScheme) const
{
auto hnInformation = fHnManager->AddHnInformation(name, fkDimension);
hnInformation->AddDimension(unitName, fcnName, binScheme);
}
//
// protected methods
//
//_____________________________________________________________________________
G4int G4H1ToolsManager::CreateH1(const G4String& name, const G4String& title,
G4int nbins, G4double xmin, G4double xmax,
const G4String& unitName, const G4String& fcnName,
const G4String& binSchemeName)
{
Message(kVL4, "create", "H1", name);
// Create H1
auto h1d
= CreateToolsH1(title, nbins, xmin, xmax, unitName, fcnName, binSchemeName,
fkClass);
// Add annotation
AddH1Annotation(h1d, unitName, fcnName);
// Save H1 information
auto binScheme = GetBinScheme(binSchemeName);
AddH1Information(name, unitName, fcnName, binScheme);
// Register histogram
auto id = RegisterT(h1d, name);
Message(kVL2, "create", "H1", name);
return id;
}
//_____________________________________________________________________________
G4int G4H1ToolsManager::CreateH1(const G4String& name, const G4String& title,
const std::vector<G4double>& edges,
const G4String& unitName, const G4String& fcnName)
{
Message(kVL4, "create", "H1", name);
auto h1d
= CreateToolsH1(title, edges, unitName, fcnName);
// Add annotation
AddH1Annotation(h1d, unitName, fcnName);
// Save H1 information
AddH1Information(name, unitName, fcnName, G4BinScheme::kUser);
// Register histogram
auto id = RegisterT(h1d, name);
Message(kVL2, "create", "H1", name);
return id;
}
//_____________________________________________________________________________
G4bool G4H1ToolsManager::SetH1(G4int id,
G4int nbins, G4double xmin, G4double xmax,
const G4String& unitName, const G4String& fcnName,
const G4String& binSchemeName)
{
auto h1d = GetTInFunction(id, "SetH1", false, false);
if ( ! h1d ) return false;
auto info = fHnManager->GetHnInformation(id,"SetH1");
Message(kVL4, "configure", "H1", info->GetName());
// Configure tools h1
ConfigureToolsH1(h1d, nbins, xmin, xmax, unitName, fcnName, binSchemeName,
fkClass);
// Add annotation
AddH1Annotation(h1d, unitName, fcnName);
// Update information
auto binScheme = GetBinScheme(binSchemeName);
UpdateH1Information(info, unitName, fcnName, binScheme);
// Set activation
fHnManager->SetActivation(id, true);
return true;
}
//_____________________________________________________________________________
G4bool G4H1ToolsManager::SetH1(G4int id,
const std::vector<G4double>& edges,
const G4String& unitName,
const G4String& fcnName)
{
auto h1d = GetTInFunction(id, "SetH1", false, false);
if ( ! h1d ) return false;
auto info = fHnManager->GetHnInformation(id,"SetH1");
Message(kVL4, "configure", "H1", info->GetName());
// Configure tools h1
ConfigureToolsH1(h1d, edges, unitName, fcnName);
// Add annotation
AddH1Annotation(h1d, unitName, fcnName);
// Update information
UpdateH1Information(info, unitName, fcnName, G4BinScheme::kUser);
// Set activation
fHnManager->SetActivation(id, true);
return true;
}
//_____________________________________________________________________________
G4bool G4H1ToolsManager::ScaleH1(G4int id, G4double factor)
{
auto h1d = GetTInFunction(id, "ScaleH1", false, false);
if ( ! h1d ) return false;
return h1d->scale(factor);
}
//_____________________________________________________________________________
G4bool G4H1ToolsManager::FillH1(G4int id, G4double value, G4double weight)
{
auto h1d = GetTInFunction(id, "FillH1", true, false);
if ( ! h1d ) return false;
if ( fState.GetIsActivation() && ( ! fHnManager->GetActivation(id) ) ) {
//G4cout << "Skipping FillH1 for " << id << G4endl;
return false;
}
auto info
= fHnManager->GetHnDimensionInformation(id, kX, "FillH1");
h1d->fill(info->fFcn(value/info->fUnit), weight);
if ( IsVerbose(kVL4) ) {
Message(kVL4, "fill", "H1",
" id " + to_string(id) + " value " + to_string(value) +
" fcn(value/unit) " + to_string(info->fFcn(value/info->fUnit)) +
" weight " + to_string(weight));
}
return true;
}
//_____________________________________________________________________________
G4int G4H1ToolsManager::GetH1Id(const G4String& name, G4bool warn) const
{
return GetTId(name, warn);
}
//_____________________________________________________________________________
G4int G4H1ToolsManager::GetH1Nbins(G4int id) const
{
auto h1d = GetTInFunction(id, "GetH1Nbins");
if ( ! h1d ) return 0;
return GetNbins(*h1d, kX);
}
//_____________________________________________________________________________
G4double G4H1ToolsManager::GetH1Xmin(G4int id) const
{
// Returns xmin value with applied unit and histogram function
auto h1d = GetTInFunction(id, "GetH1Xmin");
if ( ! h1d ) return 0.;
return GetMin(*h1d, kX);
}
//_____________________________________________________________________________
G4double G4H1ToolsManager::GetH1Xmax(G4int id) const
{
auto h1d = GetTInFunction(id, "GetH1Xmax");
if ( ! h1d ) return 0.;
return GetMax(*h1d, kX);
}
//_____________________________________________________________________________
G4double G4H1ToolsManager::GetH1Width(G4int id) const
{
auto h1d = GetTInFunction(id, "GetH1XWidth", true, false);
if ( ! h1d ) return 0.;
return GetWidth(*h1d, kX, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4bool G4H1ToolsManager::SetH1Title(G4int id, const G4String& title)
{
auto h1d = GetTInFunction(id, "SetH1Title");
if ( ! h1d ) return false;
return SetTitle(*h1d, title);
}
//_____________________________________________________________________________
G4bool G4H1ToolsManager::SetH1XAxisTitle(G4int id, const G4String& title)
{
auto h1d = GetTInFunction(id, "SetH1XAxisTitle");
if ( ! h1d ) return false;
return SetAxisTitle(*h1d, kX, title);
}
//_____________________________________________________________________________
G4bool G4H1ToolsManager::SetH1YAxisTitle(G4int id, const G4String& title)
{
auto h1d = GetTInFunction(id, "SetH1YAxisTitle");
if ( ! h1d ) return false;
return SetAxisTitle(*h1d, kY, title);
}
//_____________________________________________________________________________
G4String G4H1ToolsManager::GetH1Title(G4int id) const
{
auto h1d = GetTInFunction(id, "GetH1Title");
if ( ! h1d ) return "";
return GetTitle(*h1d);
}
//_____________________________________________________________________________
G4String G4H1ToolsManager::GetH1XAxisTitle(G4int id) const
{
auto h1d = GetTInFunction(id, "GetH1XAxisTitle");
if ( ! h1d ) return "";
return GetAxisTitle(*h1d, kX, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4String G4H1ToolsManager::GetH1YAxisTitle(G4int id) const
{
auto h1d = GetTInFunction(id, "GetH1YAxisTitle");
if ( ! h1d ) return "";
return GetAxisTitle(*h1d, kY, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4bool G4H1ToolsManager::WriteOnAscii(std::ofstream& output)
{
// Write selected objects on ASCII file
// According to the implementation by Michel Maire, originally in
// extended examples.
// Do nothing if no histograms are selected
if ( ! fHnManager->IsAscii() ) return true;
// Write h1 histograms
for ( G4int i=0; i<G4int(fTVector.size()); ++i ) {
auto id = i + fHnManager->GetFirstId();
auto info = fHnManager->GetHnInformation(id,"WriteOnAscii");
// skip writing if activation is enabled and H1 is inactivated
if ( ! info->GetAscii() ) continue;
auto h1 = fTVector[i];
Message(kVL3, "write on ascii", "h1d", info->GetName());
output << "\n 1D histogram " << id << ": " << h1->title()
<< "\n \n \t X \t\t Bin Height" << G4endl;
for (G4int j=0; j< G4int(h1->axis().bins()); ++j) {
output << " " << j << "\t"
<< h1->axis().bin_center(j) << "\t"
<< h1->bin_height(j) << G4endl;
}
}
return output.good();
}
//
// public methods
//
//_____________________________________________________________________________
G4int G4H1ToolsManager::AddH1(const G4String& name, tools::histo::h1d* h1d)
{
Message(kVL4, "add", "H1", name);
// Add annotation
AddH1Annotation(h1d, "none", "none");
// Add information
AddH1Information(name, "none", "none", G4BinScheme::kLinear);
// Register histogram
auto id = RegisterT(h1d, name);
Message(kVL2, "add", "H1", name);
return id;
}
//_____________________________________________________________________________
void G4H1ToolsManager::AddH1Vector(
const std::vector<tools::histo::h1d*>& h1Vector)
{
AddTVector(h1Vector);
}
//_____________________________________________________________________________
tools::histo::h1d* G4H1ToolsManager::GetH1(G4int id, G4bool warn,
G4bool onlyIfActive) const
{
return GetTInFunction(id, "GetH1", warn, onlyIfActive);
}
@@ -1,634 +0,0 @@
//
// ********************************************************************
// * 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, 18/06/2013 (ivana@ipno.in2p3.fr)
#include "G4H2ToolsManager.hh"
#include "G4AnalysisManagerState.hh"
#include "G4BaseHistoUtilities.hh"
#include "G4AnalysisUtilities.hh"
#include "tools/histo/h2d"
#include <fstream>
using namespace G4Analysis;
using std::to_string;
//_____________________________________________________________________________
G4H2ToolsManager::G4H2ToolsManager(const G4AnalysisManagerState& state)
: G4VH2Manager(),
G4THnManager<tools::histo::h2d>(state, "H2")
{}
//
// Utility functions
//
namespace {
//_____________________________________________________________________________
void UpdateH2Information(G4HnInformation* hnInformation,
const G4String& xunitName,
const G4String& yunitName,
const G4String& xfcnName,
const G4String& yfcnName,
G4BinScheme xbinScheme,
G4BinScheme ybinScheme)
{
hnInformation->SetDimension(kX, xunitName, xfcnName, xbinScheme);
hnInformation->SetDimension(kY, yunitName, yfcnName, ybinScheme);
}
//_____________________________________________________________________________
void AddH2Annotation(tools::histo::h2d* h2d,
const G4String& xunitName,
const G4String& yunitName,
const G4String& xfcnName,
const G4String& yfcnName)
{
G4String xaxisTitle;
G4String yaxisTitle;
UpdateTitle(xaxisTitle, xunitName, xfcnName);
UpdateTitle(yaxisTitle, yunitName, yfcnName);
h2d->add_annotation(tools::histo::key_axis_x_title(), xaxisTitle);
h2d->add_annotation(tools::histo::key_axis_y_title(), yaxisTitle);
}
//_____________________________________________________________________________
tools::histo::h2d* CreateToolsH2(
const G4String& title,
G4int nxbins, G4double xmin, G4double xmax,
G4int nybins, G4double ymin, G4double ymax,
const G4String& xunitName,
const G4String& yunitName,
const G4String& xfcnName,
const G4String& yfcnName,
const G4String& xbinSchemeName,
const G4String& ybinSchemeName,
std::string_view className)
{
auto xunit = GetUnitValue(xunitName);
auto yunit = GetUnitValue(yunitName);
auto xfcn = GetFunction(xfcnName);
auto yfcn = GetFunction(yfcnName);
auto xbinScheme = GetBinScheme(xbinSchemeName);
auto ybinScheme = GetBinScheme(ybinSchemeName);
if ( xbinScheme != G4BinScheme::kLog && ybinScheme != G4BinScheme::kLog) {
if ( xbinScheme == G4BinScheme::kUser || ybinScheme == G4BinScheme::kUser) {
// This should never happen, but let's make sure about it
// by issuing a warning
Warn("User binning scheme setting was ignored.\n"
"Linear binning will be applied with given (nbins, xmin, xmax) values.",
className, "CreateToolsH2");
}
return new tools::histo::h2d(title,
nxbins, xfcn(xmin/xunit), xfcn(xmax/xunit),
nybins, yfcn(ymin/yunit), yfcn(ymax/yunit));
// h2 objects are deleted in destructor and reset when
// closing a file.
}
else {
// Compute edges
std::vector<G4double> xedges;
ComputeEdges(nxbins, xmin, xmax, xunit, xfcn, xbinScheme, xedges);
std::vector<G4double> yedges;
ComputeEdges(nybins, ymin, ymax, yunit, yfcn, ybinScheme, yedges);
return new tools::histo::h2d(title, xedges, yedges);
}
}
//_____________________________________________________________________________
tools::histo::h2d* CreateToolsH2(
const G4String& title,
const std::vector<G4double>& xedges,
const std::vector<G4double>& yedges,
const G4String& xunitName,
const G4String& yunitName,
const G4String& xfcnName,
const G4String& yfcnName)
{
auto xunit = GetUnitValue(xunitName);
auto yunit = GetUnitValue(yunitName);
auto xfcn = GetFunction(xfcnName);
auto yfcn = GetFunction(yfcnName);
// Apply function
std::vector<G4double> xnewEdges;
ComputeEdges(xedges, xunit, xfcn, xnewEdges);
std::vector<G4double> ynewEdges;
ComputeEdges(yedges, yunit, yfcn, ynewEdges);
return new tools::histo::h2d(title, xnewEdges, ynewEdges);
// h2 objects are deleted in destructor and reset when
// closing a file.
}
//_____________________________________________________________________________
void ConfigureToolsH2(tools::histo::h2d* h2d,
G4int nxbins, G4double xmin, G4double xmax,
G4int nybins, G4double ymin, G4double ymax,
const G4String& xunitName,
const G4String& yunitName,
const G4String& xfcnName,
const G4String& yfcnName,
const G4String& xbinSchemeName,
const G4String& ybinSchemeName,
std::string_view className)
{
auto xunit = GetUnitValue(xunitName);
auto yunit = GetUnitValue(yunitName);
auto xfcn = GetFunction(xfcnName);
auto yfcn = GetFunction(yfcnName);
auto xbinScheme = GetBinScheme(xbinSchemeName);
auto ybinScheme = GetBinScheme(ybinSchemeName);
if ( xbinScheme != G4BinScheme::kLog && ybinScheme != G4BinScheme::kLog) {
if ( xbinScheme == G4BinScheme::kUser || ybinScheme == G4BinScheme::kUser) {
// This should never happen, but let's make sure about it
// by issuing a warning
Warn("User binning scheme setting was ignored.\n"
"Linear binning will be applied with given (nbins, xmin, xmax) values.",
className, "ConfigureToolsH2");
}
h2d->configure(nxbins, xfcn(xmin/xunit), xfcn(xmax/xunit),
nybins, yfcn(ymin/yunit), yfcn(ymax/yunit));
}
else {
// Compute bins
std::vector<G4double> xedges;
ComputeEdges(nxbins, xmin, xmax, xunit, xfcn, xbinScheme, xedges);
std::vector<G4double> yedges;
ComputeEdges(nybins, ymin, ymax, yunit, yfcn, ybinScheme, yedges);
h2d->configure(xedges, yedges);
}
}
//_____________________________________________________________________________
void ConfigureToolsH2(tools::histo::h2d* h2d,
const std::vector<G4double>& xedges,
const std::vector<G4double>& yedges,
const G4String& xunitName,
const G4String& yunitName,
const G4String& xfcnName,
const G4String& yfcnName)
{
auto xunit = GetUnitValue(xunitName);
auto xfcn = GetFunction(xfcnName);
std::vector<G4double> xnewEdges;
ComputeEdges(xedges, xunit, xfcn, xnewEdges);
auto yunit = GetUnitValue(yunitName);
auto yfcn = GetFunction(yfcnName);
std::vector<G4double> ynewEdges;
ComputeEdges(yedges, yunit, yfcn, ynewEdges);
h2d->configure(xnewEdges, ynewEdges);
}
}
//
// private methods
//
//_____________________________________________________________________________
void G4H2ToolsManager::AddH2Information(const G4String& name,
const G4String& xunitName,
const G4String& yunitName,
const G4String& xfcnName,
const G4String& yfcnName,
G4BinScheme xbinScheme,
G4BinScheme ybinScheme) const
{
auto hnInformation = fHnManager->AddHnInformation(name, fkDimension);
hnInformation->AddDimension(xunitName, xfcnName, xbinScheme);
hnInformation->AddDimension(yunitName, yfcnName, ybinScheme);
}
//
// protected methods
//
//_____________________________________________________________________________
G4int G4H2ToolsManager::CreateH2(const G4String& name, const G4String& title,
G4int nxbins, G4double xmin, G4double xmax,
G4int nybins, G4double ymin, G4double ymax,
const G4String& xunitName, const G4String& yunitName,
const G4String& xfcnName, const G4String& yfcnName,
const G4String& xbinSchemeName,
const G4String& ybinSchemeName)
{
Message(kVL4, "create", "H2", name);
tools::histo::h2d* h2d
= CreateToolsH2(title, nxbins, xmin, xmax, nybins, ymin, ymax,
xunitName, yunitName, xfcnName, yfcnName,
xbinSchemeName, ybinSchemeName, fkClass);
// Add annotation
AddH2Annotation(h2d, xunitName, yunitName, xfcnName, yfcnName);
// Save H2 information
auto xbinScheme = GetBinScheme(xbinSchemeName);
auto ybinScheme = GetBinScheme(ybinSchemeName);
AddH2Information(
name, xunitName, yunitName, xfcnName, yfcnName, xbinScheme, ybinScheme);
// Register histogram
G4int id = RegisterT(h2d, name);
Message(kVL2, "create", "H2", name);
return id;
}
//_____________________________________________________________________________
G4int G4H2ToolsManager::CreateH2(const G4String& name, const G4String& title,
const std::vector<G4double>& xedges,
const std::vector<G4double>& yedges,
const G4String& xunitName, const G4String& yunitName,
const G4String& xfcnName, const G4String& yfcnName)
{
Message(kVL4, "create", "H2", name);
tools::histo::h2d* h2d
= CreateToolsH2(title, xedges, yedges,
xunitName, yunitName, xfcnName, yfcnName);
// Add annotation
AddH2Annotation(h2d, xunitName, yunitName, xfcnName, yfcnName);
// Save H2 information
AddH2Information(
name, xunitName, yunitName, xfcnName, yfcnName, G4BinScheme::kUser, G4BinScheme::kUser);
// Register histogram
G4int id = RegisterT(h2d, name);
Message(kVL2, "create", "H2", name);
return id;
}
//_____________________________________________________________________________
G4bool G4H2ToolsManager::SetH2(G4int id,
G4int nxbins, G4double xmin, G4double xmax,
G4int nybins, G4double ymin, G4double ymax,
const G4String& xunitName, const G4String& yunitName,
const G4String& xfcnName, const G4String& yfcnName,
const G4String& xbinSchemeName,
const G4String& ybinSchemeName)
{
auto h2d = GetTInFunction(id, "SetH2", false, false);
if ( ! h2d ) return false;
auto info = fHnManager->GetHnInformation(id, "SetH2");
Message(kVL4, "configure", "H2", info->GetName());
// Configure tools h2
ConfigureToolsH2(
h2d, nxbins, xmin, xmax, nybins, ymin, ymax,
xunitName, yunitName, xfcnName, yfcnName, xbinSchemeName, ybinSchemeName,
fkClass);
// Add annotation
AddH2Annotation(h2d, xunitName, yunitName, xfcnName, yfcnName);
// Update information
auto xbinScheme = GetBinScheme(xbinSchemeName);
auto ybinScheme = GetBinScheme(ybinSchemeName);
UpdateH2Information(
info, xunitName, yunitName, xfcnName, yfcnName, xbinScheme, ybinScheme);
// Set activation
fHnManager->SetActivation(id, true);
return true;
}
//_____________________________________________________________________________
G4bool G4H2ToolsManager::SetH2(G4int id,
const std::vector<G4double>& xedges,
const std::vector<G4double>& yedges,
const G4String& xunitName, const G4String& yunitName,
const G4String& xfcnName, const G4String& yfcnName)
{
auto h2d = GetTInFunction(id, "SetH2", false, false);
if ( ! h2d ) return false;
auto info = fHnManager->GetHnInformation(id, "SetH2");
Message(kVL4, "configure", "H2", info->GetName());
// Configure tools h2
ConfigureToolsH2(h2d, xedges, yedges, xunitName, yunitName, xfcnName, yfcnName);
// Add annotation
AddH2Annotation(h2d, xunitName, yunitName, xfcnName, yfcnName);
// Update information
UpdateH2Information(
info, xunitName, yunitName, xfcnName, yfcnName, G4BinScheme::kUser, G4BinScheme::kUser);
// Set activation
fHnManager->SetActivation(id, true);
return true;
}
//_____________________________________________________________________________
G4bool G4H2ToolsManager::ScaleH2(G4int id, G4double factor)
{
auto h2d = GetTInFunction(id, "ScaleH2", false, false);
if ( ! h2d ) return false;
return h2d->scale(factor);
}
//_____________________________________________________________________________
G4bool G4H2ToolsManager::FillH2(G4int id,
G4double xvalue, G4double yvalue,
G4double weight)
{
auto h2d = GetTInFunction(id, "FillH2", true, false);
if ( ! h2d ) return false;
if ( fState.GetIsActivation() && ( ! fHnManager->GetActivation(id) ) ) {
return false;
}
G4HnDimensionInformation* xInfo
= fHnManager->GetHnDimensionInformation(id, kX, "FillH2");
G4HnDimensionInformation* yInfo
= fHnManager->GetHnDimensionInformation(id, kY, "FillH2");
h2d->fill(xInfo->fFcn(xvalue/xInfo->fUnit),
yInfo->fFcn(yvalue/yInfo->fUnit), weight);
if ( IsVerbose(kVL4) ) {
Message(kVL4, "fill", "H2",
" id " + to_string(id) +
" xvalue " + to_string(xvalue) +
" xfcn(xvalue/xunit) " + to_string(xInfo->fFcn(xvalue/xInfo->fUnit)) +
" yvalue " + to_string(yvalue) +
" yfcn(yvalue/yunit) " + to_string(yInfo->fFcn(yvalue/yInfo->fUnit)) +
" weight " + to_string(weight));
}
return true;
}
//_____________________________________________________________________________
G4int G4H2ToolsManager::GetH2Id(const G4String& name, G4bool warn) const
{
return GetTId(name, warn);
}
//_____________________________________________________________________________
G4int G4H2ToolsManager::GetH2Nxbins(G4int id) const
{
auto h2d = GetTInFunction(id, "GetH2NXbins");
if ( ! h2d ) return 0;
return GetNbins(*h2d, kX);
}
//_____________________________________________________________________________
G4double G4H2ToolsManager::GetH2Xmin(G4int id) const
{
// Returns xmin value with applied unit and histogram function
auto h2d = GetTInFunction(id, "GetH2Xmin");
if ( ! h2d ) return 0.;
return GetMin(*h2d, kX);
}
//_____________________________________________________________________________
G4double G4H2ToolsManager::GetH2Xmax(G4int id) const
{
auto h2d = GetTInFunction(id, "GetH2Xmax");
if ( ! h2d ) return 0.;
return GetMax(*h2d, kX);
}
//_____________________________________________________________________________
G4double G4H2ToolsManager::GetH2XWidth(G4int id) const
{
auto h2d = GetTInFunction(id, "GetH2XWidth", true, false);
if ( ! h2d ) return 0.;
return GetWidth(*h2d, kX, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4int G4H2ToolsManager::GetH2Nybins(G4int id) const
{
auto h2d = GetTInFunction(id, "GetH2NYbins");
if ( ! h2d ) return 0;
return GetNbins(*h2d, kY);
}
//_____________________________________________________________________________
G4double G4H2ToolsManager::GetH2Ymin(G4int id) const
{
// Returns xmin value with applied unit and histogram function
auto h2d = GetTInFunction(id, "GetH2Ymin");
if ( ! h2d ) return 0.;
return GetMin(*h2d, kY);
}
//_____________________________________________________________________________
G4double G4H2ToolsManager::GetH2Ymax(G4int id) const
{
auto h2d = GetTInFunction(id, "GetH2Ymax");
if ( ! h2d ) return 0.;
return GetMax(*h2d, kY);
}
//_____________________________________________________________________________
G4double G4H2ToolsManager::GetH2YWidth(G4int id) const
{
auto h2d = GetTInFunction(id, "GetH2YWidth", true, false);
if ( ! h2d ) return 0.;
return GetWidth(*h2d, kY, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4bool G4H2ToolsManager::SetH2Title(G4int id, const G4String& title)
{
auto h2d = GetTInFunction(id, "SetH2Title");
if ( ! h2d ) return false;
return SetTitle(*h2d, title);
}
//_____________________________________________________________________________
G4bool G4H2ToolsManager::SetH2XAxisTitle(G4int id, const G4String& title)
{
auto h2d = GetTInFunction(id, "SetH2XAxisTitle");
if ( ! h2d ) return false;
return SetAxisTitle(*h2d, kX, title);
}
//_____________________________________________________________________________
G4bool G4H2ToolsManager::SetH2YAxisTitle(G4int id, const G4String& title)
{
auto h2d = GetTInFunction(id, "SetH2YAxisTitle");
if ( ! h2d ) return false;
return SetAxisTitle(*h2d, kY, title);
}
//_____________________________________________________________________________
G4bool G4H2ToolsManager::SetH2ZAxisTitle(G4int id, const G4String& title)
{
auto h2d = GetTInFunction(id, "SetH2ZAxisTitle");
if ( ! h2d ) return false;
return SetAxisTitle(*h2d, kZ, title);
}
//_____________________________________________________________________________
G4String G4H2ToolsManager::GetH2Title(G4int id) const
{
auto h2d = GetTInFunction(id, "GetH2Title");
if ( ! h2d ) return "";
return GetTitle(*h2d);
}
//_____________________________________________________________________________
G4String G4H2ToolsManager::GetH2XAxisTitle(G4int id) const
{
auto h2d = GetTInFunction(id, "GetH2XAxisTitle");
if ( ! h2d ) return "";
return GetAxisTitle(*h2d, kX, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4String G4H2ToolsManager::GetH2YAxisTitle(G4int id) const
{
auto h2d = GetTInFunction(id, "GetH2YAxisTitle");
if ( ! h2d ) return "";
return GetAxisTitle(*h2d, kY, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4String G4H2ToolsManager::GetH2ZAxisTitle(G4int id) const
{
auto h2d = GetTInFunction(id, "GetH2ZAxisTitle");
if ( ! h2d ) return "";
return GetAxisTitle(*h2d, kZ, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4bool G4H2ToolsManager::WriteOnAscii(std::ofstream& output)
{
// Write selected objects on ASCII file
// Do nothing if no histograms are selected
if ( ! fHnManager->IsAscii() ) return true;
// Write h2 histograms
for ( G4int i=0; i<G4int(fTVector.size()); ++i ) {
auto id = i + fHnManager->GetFirstId();
auto info = fHnManager->GetHnInformation(id,"WriteOnAscii");
// skip writing if activation is enabled and H1 is inactivated
if ( ! info->GetAscii() ) continue;
auto h2 = fTVector[i];
Message(kVL3, "write on ascii", "h2d", info->GetName());
output << "\n 2D histogram " << id << ": " << h2->title()
<< "\n \n \t \t X \t\t Y \t\t Bin Height" << G4endl;
for (G4int j=0; j< G4int(h2->axis_x().bins()); ++j) {
for (G4int k=0; k< G4int(h2->axis_y().bins()); ++k) {
output << " " << j << "\t" << k << "\t"
<< h2->axis_x().bin_center(j) << "\t"
<< h2->axis_y().bin_center(k) << "\t"
<< h2->bin_height(j, k) << G4endl;
}
}
}
return output.good();
}
//
// public methods
//
//_____________________________________________________________________________
G4int G4H2ToolsManager::AddH2(const G4String& name, tools::histo::h2d* h2d)
{
Message(kVL4, "add", "H2", name);
// Add annotation
AddH2Annotation(h2d, "none", "none", "none", "none");
// Add information
AddH2Information(name, "none", "none", "none", "none",
G4BinScheme::kLinear, G4BinScheme::kLinear);
// Register histogram
G4int id = RegisterT(h2d, name);
Message(kVL2, "add", "H2", name);
return id;
}
//_____________________________________________________________________________
void G4H2ToolsManager::AddH2Vector(
const std::vector<tools::histo::h2d*>& h2Vector)
{
AddTVector(h2Vector);
}
//_____________________________________________________________________________
tools::histo::h2d* G4H2ToolsManager::GetH2(G4int id, G4bool warn,
G4bool onlyIfActive) const
{
return GetTInFunction(id, "GetH2", warn, onlyIfActive);
}
@@ -1,755 +0,0 @@
//
// ********************************************************************
// * 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, 24/07/2014 (ivana@ipno.in2p3.fr)
#include "G4H3ToolsManager.hh"
#include "G4AnalysisManagerState.hh"
#include "G4BaseHistoUtilities.hh"
#include "G4AnalysisUtilities.hh"
#include "tools/histo/h3d"
#include <fstream>
using namespace G4Analysis;
using std::to_string;
//_____________________________________________________________________________
G4H3ToolsManager::G4H3ToolsManager(const G4AnalysisManagerState& state)
: G4VH3Manager(),
G4THnManager<tools::histo::h3d>(state, "H3")
{}
//
// Utility functions
//
namespace {
//_____________________________________________________________________________
void UpdateH3Information(G4HnInformation* hnInformation,
const G4String& xunitName,
const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName,
const G4String& yfcnName,
const G4String& zfcnName,
G4BinScheme xbinScheme,
G4BinScheme ybinScheme,
G4BinScheme zbinScheme)
{
hnInformation->SetDimension(kX, xunitName, xfcnName, xbinScheme);
hnInformation->SetDimension(kY, yunitName, yfcnName, ybinScheme);
hnInformation->SetDimension(kZ, zunitName, zfcnName, zbinScheme);
}
//_____________________________________________________________________________
void AddH3Annotation(tools::histo::h3d* h3d,
const G4String& xunitName,
const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName,
const G4String& yfcnName,
const G4String& zfcnName)
{
G4String xaxisTitle;
G4String yaxisTitle;
G4String zaxisTitle;
UpdateTitle(xaxisTitle, xunitName, xfcnName);
UpdateTitle(yaxisTitle, yunitName, yfcnName);
UpdateTitle(zaxisTitle, zunitName, zfcnName);
h3d->add_annotation(tools::histo::key_axis_x_title(), xaxisTitle);
h3d->add_annotation(tools::histo::key_axis_y_title(), yaxisTitle);
h3d->add_annotation(tools::histo::key_axis_z_title(), zaxisTitle);
}
//_____________________________________________________________________________
tools::histo::h3d* CreateToolsH3(
const G4String& title,
G4int nxbins, G4double xmin, G4double xmax,
G4int nybins, G4double ymin, G4double ymax,
G4int nzbins, G4double zmin, G4double zmax,
const G4String& xunitName,
const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName,
const G4String& zfcnName,
const G4String& yfcnName,
const G4String& xbinSchemeName,
const G4String& ybinSchemeName,
const G4String& zbinSchemeName,
std::string_view className)
{
auto xunit = GetUnitValue(xunitName);
auto yunit = GetUnitValue(yunitName);
auto zunit = GetUnitValue(zunitName);
auto xfcn = GetFunction(xfcnName);
auto yfcn = GetFunction(yfcnName);
auto zfcn = GetFunction(zfcnName);
auto xbinScheme = GetBinScheme(xbinSchemeName);
auto ybinScheme = GetBinScheme(ybinSchemeName);
auto zbinScheme = GetBinScheme(zbinSchemeName);
if ( xbinScheme != G4BinScheme::kLog && ybinScheme != G4BinScheme::kLog && zbinScheme != G4BinScheme::kLog) {
if ( xbinScheme == G4BinScheme::kUser || ybinScheme == G4BinScheme::kUser || zbinScheme == G4BinScheme::kUser) {
// This should never happen, but let's make sure about it
// by issuing a warning
Warn("User binning scheme setting was ignored.\n"
"Linear binning will be applied with given (nbins, xmin, xmax) values.",
className, "CreateToolsH3");
}
return new tools::histo::h3d(title,
nxbins, xfcn(xmin/xunit), xfcn(xmax/xunit),
nybins, yfcn(ymin/yunit), yfcn(ymax/yunit),
nzbins, zfcn(zmin/zunit), zfcn(zmax/zunit));
// h3 objects are deleted in destructor and reset when
// closing a file.
}
else {
// Compute edges
std::vector<G4double> xedges;
ComputeEdges(nxbins, xmin, xmax, xunit, xfcn, xbinScheme, xedges);
std::vector<G4double> yedges;
ComputeEdges(nybins, ymin, ymax, yunit, yfcn, ybinScheme, yedges);
std::vector<G4double> zedges;
ComputeEdges(nzbins, zmin, zmax, zunit, zfcn, zbinScheme, zedges);
return new tools::histo::h3d(title, xedges, yedges, zedges);
}
}
//_____________________________________________________________________________
tools::histo::h3d* CreateToolsH3(
const G4String& title,
const std::vector<G4double>& xedges,
const std::vector<G4double>& yedges,
const std::vector<G4double>& zedges,
const G4String& xunitName,
const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName,
const G4String& yfcnName,
const G4String& zfcnName)
{
auto xunit = GetUnitValue(xunitName);
auto yunit = GetUnitValue(yunitName);
auto zunit = GetUnitValue(zunitName);
auto xfcn = GetFunction(xfcnName);
auto yfcn = GetFunction(yfcnName);
auto zfcn = GetFunction(zfcnName);
// Apply function
std::vector<G4double> xnewEdges;
ComputeEdges(xedges, xunit, xfcn, xnewEdges);
std::vector<G4double> ynewEdges;
ComputeEdges(yedges, yunit, yfcn, ynewEdges);
std::vector<G4double> znewEdges;
ComputeEdges(zedges, zunit, zfcn, znewEdges);
return new tools::histo::h3d(title, xnewEdges, ynewEdges, znewEdges);
// h3 objects are deleted in destructor and reset when
// closing a file.
}
//_____________________________________________________________________________
void ConfigureToolsH3(tools::histo::h3d* h3d,
G4int nxbins, G4double xmin, G4double xmax,
G4int nybins, G4double ymin, G4double ymax,
G4int nzbins, G4double zmin, G4double zmax,
const G4String& xunitName,
const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName,
const G4String& yfcnName,
const G4String& zfcnName,
const G4String& xbinSchemeName,
const G4String& ybinSchemeName,
const G4String& zbinSchemeName,
std::string_view className)
{
auto xunit = GetUnitValue(xunitName);
auto yunit = GetUnitValue(yunitName);
auto zunit = GetUnitValue(zunitName);
auto xfcn = GetFunction(xfcnName);
auto yfcn = GetFunction(yfcnName);
auto zfcn = GetFunction(zfcnName);
auto xbinScheme = GetBinScheme(xbinSchemeName);
auto ybinScheme = GetBinScheme(ybinSchemeName);
auto zbinScheme = GetBinScheme(zbinSchemeName);
if ( xbinScheme != G4BinScheme::kLog && ybinScheme != G4BinScheme::kLog && zbinScheme != G4BinScheme::kLog) {
if ( xbinScheme == G4BinScheme::kUser || ybinScheme == G4BinScheme::kUser || zbinScheme == G4BinScheme::kUser) {
// This should never happen, but let's make sure about it
// by issuing a warning
Warn("User binning scheme setting was ignored.\n"
"Linear binning will be applied with given (nbins, xmin, xmax) values.",
className, "ConfigureToolsH3");
}
h3d->configure(nxbins, xfcn(xmin/xunit), xfcn(xmax/xunit),
nybins, yfcn(ymin/yunit), yfcn(ymax/yunit),
nzbins, zfcn(zmin/zunit), zfcn(zmax/zunit));
}
else {
// Compute bins
std::vector<G4double> xedges;
ComputeEdges(nxbins, xmin, xmax, xunit, xfcn, xbinScheme, xedges);
std::vector<G4double> yedges;
ComputeEdges(nybins, ymin, ymax, yunit, yfcn, ybinScheme, yedges);
std::vector<G4double> zedges;
ComputeEdges(nzbins, zmin, zmax, zunit, zfcn, zbinScheme, zedges);
h3d->configure(xedges, yedges, zedges);
}
}
//_____________________________________________________________________________
void ConfigureToolsH3(tools::histo::h3d* h3d,
const std::vector<G4double>& xedges,
const std::vector<G4double>& yedges,
const std::vector<G4double>& zedges,
const G4String& xunitName,
const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName,
const G4String& yfcnName,
const G4String& zfcnName)
{
auto xunit = GetUnitValue(xunitName);
auto xfcn = GetFunction(xfcnName);
std::vector<G4double> xnewEdges;
ComputeEdges(xedges, xunit, xfcn, xnewEdges);
auto yunit = GetUnitValue(yunitName);
auto yfcn = GetFunction(yfcnName);
std::vector<G4double> ynewEdges;
ComputeEdges(yedges, yunit, yfcn, ynewEdges);
auto zunit = GetUnitValue(zunitName);
auto zfcn = GetFunction(zfcnName);
std::vector<G4double> znewEdges;
ComputeEdges(zedges, zunit, zfcn, znewEdges);
h3d->configure(xnewEdges, ynewEdges, znewEdges);
}
}
//
// private methods
//
//_____________________________________________________________________________
void G4H3ToolsManager::AddH3Information(const G4String& name,
const G4String& xunitName,
const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName,
const G4String& yfcnName,
const G4String& zfcnName,
G4BinScheme xbinScheme,
G4BinScheme ybinScheme,
G4BinScheme zbinScheme) const
{
auto hnInformation = fHnManager->AddHnInformation(name, fkDimension);
hnInformation->AddDimension(xunitName, xfcnName, xbinScheme);
hnInformation->AddDimension(yunitName, yfcnName, ybinScheme);
hnInformation->AddDimension(zunitName, zfcnName, zbinScheme);
}
//
// protected methods
//
//_____________________________________________________________________________
G4int G4H3ToolsManager::CreateH3(const G4String& name, const G4String& title,
G4int nxbins, G4double xmin, G4double xmax,
G4int nybins, G4double ymin, G4double ymax,
G4int nzbins, G4double zmin, G4double zmax,
const G4String& xunitName, const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName, const G4String& yfcnName,
const G4String& zfcnName,
const G4String& xbinSchemeName,
const G4String& ybinSchemeName,
const G4String& zbinSchemeName)
{
Message(kVL4, "create", "H3", name);
tools::histo::h3d* h3d
= CreateToolsH3(title,
nxbins, xmin, xmax, nybins, ymin, ymax, nzbins, zmin, zmax,
xunitName, yunitName, zunitName,
xfcnName, yfcnName, zfcnName,
xbinSchemeName, ybinSchemeName, zbinSchemeName, fkClass);
// Add annotation
AddH3Annotation(h3d, xunitName, yunitName, zunitName,
xfcnName, yfcnName, zfcnName);
// Save H3 information
auto xbinScheme = GetBinScheme(xbinSchemeName);
auto ybinScheme = GetBinScheme(ybinSchemeName);
auto zbinScheme = GetBinScheme(zbinSchemeName);
AddH3Information(
name, xunitName, yunitName, zunitName, xfcnName, yfcnName, zfcnName,
xbinScheme, ybinScheme, zbinScheme);
// Register histogram
G4int id = RegisterT(h3d, name);
Message(kVL2, "create", "H3", name);
return id;
}
//_____________________________________________________________________________
G4int G4H3ToolsManager::CreateH3(const G4String& name, const G4String& title,
const std::vector<G4double>& xedges,
const std::vector<G4double>& yedges,
const std::vector<G4double>& zedges,
const G4String& xunitName, const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName, const G4String& yfcnName,
const G4String& zfcnName)
{
Message(kVL4, "create", "H3", name);
tools::histo::h3d* h3d
= CreateToolsH3(title, xedges, yedges, zedges,
xunitName, yunitName, zunitName, xfcnName, yfcnName, zfcnName);
// Add annotation
AddH3Annotation(h3d, xunitName, yunitName, zunitName,
xfcnName, yfcnName, zfcnName);
// Save H3 information
AddH3Information(
name, xunitName, yunitName, zunitName, xfcnName, yfcnName, zfcnName,
G4BinScheme::kUser, G4BinScheme::kUser, G4BinScheme::kUser);
// Register histogram
G4int id = RegisterT(h3d, name);
Message(kVL2, "create", "H3", name);
return id;
}
//_____________________________________________________________________________
G4bool G4H3ToolsManager::SetH3(G4int id,
G4int nxbins, G4double xmin, G4double xmax,
G4int nybins, G4double ymin, G4double ymax,
G4int nzbins, G4double zmin, G4double zmax,
const G4String& xunitName, const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName, const G4String& yfcnName,
const G4String& zfcnName,
const G4String& xbinSchemeName,
const G4String& ybinSchemeName,
const G4String& zbinSchemeName)
{
auto h3d = GetTInFunction(id, "SetH3", false, false);
if ( ! h3d ) return false;
auto info = fHnManager->GetHnInformation(id, "SetH3");
Message(kVL4, "configure", "H3", info->GetName());
// Configure tools h3
ConfigureToolsH3(
h3d, nxbins, xmin, xmax, nybins, ymin, ymax, nzbins, zmin, zmax,
xunitName, yunitName, zunitName, xfcnName, yfcnName, zfcnName,
xbinSchemeName, ybinSchemeName, zbinSchemeName, fkClass);
// Add annotation
AddH3Annotation(h3d, xunitName, yunitName, zunitName,
xfcnName, yfcnName, zfcnName);
// Update information
auto xbinScheme = GetBinScheme(xbinSchemeName);
auto ybinScheme = GetBinScheme(ybinSchemeName);
auto zbinScheme = GetBinScheme(zbinSchemeName);
UpdateH3Information(
info, xunitName, yunitName, zunitName, xfcnName, yfcnName, zfcnName,
xbinScheme, ybinScheme, zbinScheme);
// Set activation
fHnManager->SetActivation(id, true);
return true;
}
//_____________________________________________________________________________
G4bool G4H3ToolsManager::SetH3(G4int id,
const std::vector<G4double>& xedges,
const std::vector<G4double>& yedges,
const std::vector<G4double>& zedges,
const G4String& xunitName, const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName, const G4String& yfcnName,
const G4String& zfcnName)
{
auto h3d = GetTInFunction(id, "SetH3", false, false);
if ( ! h3d ) return false;
auto info = fHnManager->GetHnInformation(id, "SetH3");
Message(kVL4, "configure", "H3", info->GetName());
// Configure tools h3
ConfigureToolsH3(h3d, xedges, yedges, zedges,
xunitName, yunitName, zunitName, xfcnName, yfcnName, zfcnName);
// Add annotation
AddH3Annotation(h3d, xunitName, yunitName, zunitName,
xfcnName, yfcnName, zfcnName);
// Update information
UpdateH3Information(
info, xunitName, yunitName, zunitName, xfcnName, yfcnName, zfcnName,
G4BinScheme::kUser, G4BinScheme::kUser, G4BinScheme::kUser);
// Set activation
fHnManager->SetActivation(id, true);
return true;
}
//_____________________________________________________________________________
G4bool G4H3ToolsManager::ScaleH3(G4int id, G4double factor)
{
auto h3d = GetTInFunction(id, "ScaleH3", false, false);
if ( ! h3d ) return false;
return h3d->scale(factor);
}
//_____________________________________________________________________________
G4bool G4H3ToolsManager::FillH3(G4int id,
G4double xvalue, G4double yvalue, G4double zvalue,
G4double weight)
{
auto h3d = GetTInFunction(id, "FillH3", true, false);
if ( ! h3d ) return false;
if ( fState.GetIsActivation() && ( ! fHnManager->GetActivation(id) ) ) {
return false;
}
G4HnDimensionInformation* xInfo
= fHnManager->GetHnDimensionInformation(id, kX, "FillH3");
G4HnDimensionInformation* yInfo
= fHnManager->GetHnDimensionInformation(id, kY, "FillH3");
G4HnDimensionInformation* zInfo
= fHnManager->GetHnDimensionInformation(id, kZ, "FillH3");
h3d->fill(xInfo->fFcn(xvalue/xInfo->fUnit),
yInfo->fFcn(yvalue/yInfo->fUnit),
zInfo->fFcn(zvalue/zInfo->fUnit), weight);
if ( IsVerbose(kVL4) ) {
Message(kVL4, "fill", "H3",
" id " + to_string(id) +
" xvalue " + to_string(xvalue) +
" xfcn(xvalue/xunit) " + to_string(xInfo->fFcn(xvalue/xInfo->fUnit)) +
" yvalue " + to_string(yvalue) +
" yfcn(yvalue/yunit) " + to_string(yInfo->fFcn(yvalue/yInfo->fUnit)) +
" zvalue " + to_string(zvalue) +
" zfcn(zvalue/zunit) " + to_string(zInfo->fFcn(zvalue/zInfo->fUnit)) +
" weight " + to_string(weight));
}
return true;
}
//_____________________________________________________________________________
G4int G4H3ToolsManager::GetH3Id(const G4String& name, G4bool warn) const
{
return GetTId(name, warn);
}
//_____________________________________________________________________________
G4int G4H3ToolsManager::GetH3Nxbins(G4int id) const
{
auto h3d = GetTInFunction(id, "GetH3NXbins");
if ( ! h3d ) return 0;
return GetNbins(*h3d, kX);
}
//_____________________________________________________________________________
G4double G4H3ToolsManager::GetH3Xmin(G4int id) const
{
// Returns xmin value with applied unit and histogram function
auto h3d = GetTInFunction(id, "GetH3Xmin");
if ( ! h3d ) return 0.;
return GetMin(*h3d, kX);
}
//_____________________________________________________________________________
G4double G4H3ToolsManager::GetH3Xmax(G4int id) const
{
auto h3d = GetTInFunction(id, "GetH3Xmax");
if ( ! h3d ) return 0.;
return GetMax(*h3d, kX);
}
//_____________________________________________________________________________
G4double G4H3ToolsManager::GetH3XWidth(G4int id) const
{
auto h3d = GetTInFunction(id, "GetH3XWidth", true, false);
if ( ! h3d ) return 0.;
return GetWidth(*h3d, kX, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4int G4H3ToolsManager::GetH3Nybins(G4int id) const
{
auto h3d = GetTInFunction(id, "GetH3NYbins");
if ( ! h3d ) return 0;
return GetNbins(*h3d, kY);
}
//_____________________________________________________________________________
G4double G4H3ToolsManager::GetH3Ymin(G4int id) const
{
// Returns xmin value with applied unit and histogram function
auto h3d = GetTInFunction(id, "GetH3Ymin");
if ( ! h3d ) return 0.;
return GetMin(*h3d, kY);
}
//_____________________________________________________________________________
G4double G4H3ToolsManager::GetH3Ymax(G4int id) const
{
auto h3d = GetTInFunction(id, "GetH3Ymax");
if ( ! h3d ) return 0.;
return GetMax(*h3d, kY);
}
//_____________________________________________________________________________
G4double G4H3ToolsManager::GetH3YWidth(G4int id) const
{
auto h3d = GetTInFunction(id, "GetH3YWidth", true, false);
if ( ! h3d ) return 0.;
return GetWidth(*h3d, kY, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4int G4H3ToolsManager::GetH3Nzbins(G4int id) const
{
auto h3d = GetTInFunction(id, "GetH3NZbins");
if ( ! h3d ) return 0;
return GetNbins(*h3d, kZ);
}
//_____________________________________________________________________________
G4double G4H3ToolsManager::GetH3Zmin(G4int id) const
{
// Returns xmin value with applied unit and histogram function
auto h3d = GetTInFunction(id, "GetH3Zmin");
if ( ! h3d ) return 0.;
return GetMin(*h3d, kZ);
}
//_____________________________________________________________________________
G4double G4H3ToolsManager::GetH3Zmax(G4int id) const
{
auto h3d = GetTInFunction(id, "GetH3Zmax");
if ( ! h3d ) return 0.;
return GetMax(*h3d, kZ);
}
//_____________________________________________________________________________
G4double G4H3ToolsManager::GetH3ZWidth(G4int id) const
{
auto h3d = GetTInFunction(id, "GetH3ZWidth", true, false);
if ( ! h3d ) return 0.;
return GetWidth(*h3d, kZ, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4bool G4H3ToolsManager::SetH3Title(G4int id, const G4String& title)
{
auto h3d = GetTInFunction(id, "SetH3Title");
if ( ! h3d ) return false;
return SetTitle(*h3d, title);
}
//_____________________________________________________________________________
G4bool G4H3ToolsManager::SetH3XAxisTitle(G4int id, const G4String& title)
{
auto h3d = GetTInFunction(id, "SetH3XAxisTitle");
if ( ! h3d ) return false;
return SetAxisTitle(*h3d, kX, title);
}
//_____________________________________________________________________________
G4bool G4H3ToolsManager::SetH3YAxisTitle(G4int id, const G4String& title)
{
auto h3d = GetTInFunction(id, "SetH3YAxisTitle");
if ( ! h3d ) return false;
return SetAxisTitle(*h3d, kY, title);
}
//_____________________________________________________________________________
G4bool G4H3ToolsManager::SetH3ZAxisTitle(G4int id, const G4String& title)
{
auto h3d = GetTInFunction(id, "SetH3ZAxisTitle");
if ( ! h3d ) return false;
return SetAxisTitle(*h3d, kZ, title);
}
//_____________________________________________________________________________
G4String G4H3ToolsManager::GetH3Title(G4int id) const
{
auto h3d = GetTInFunction(id, "GetH3Title");
if ( ! h3d ) return "";
return GetTitle(*h3d);
}
//_____________________________________________________________________________
G4String G4H3ToolsManager::GetH3XAxisTitle(G4int id) const
{
auto h3d = GetTInFunction(id, "GetH3XAxisTitle");
if ( ! h3d ) return "";
return GetAxisTitle(*h3d, kX, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4String G4H3ToolsManager::GetH3YAxisTitle(G4int id) const
{
auto h3d = GetTInFunction(id, "GetH3YAxisTitle");
if ( ! h3d ) return "";
return GetAxisTitle(*h3d, kY, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4String G4H3ToolsManager::GetH3ZAxisTitle(G4int id) const
{
auto h3d = GetTInFunction(id, "GetH3ZAxisTitle");
if ( ! h3d ) return "";
return GetAxisTitle(*h3d, kZ, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4bool G4H3ToolsManager::WriteOnAscii(std::ofstream& output)
{
// Write selected objects on ASCII file
// Do nothing if no histograms are selected
if ( ! fHnManager->IsAscii() ) return true;
// Write h3 histograms
for ( G4int i=0; i<G4int(fTVector.size()); ++i ) {
auto id = i + fHnManager->GetFirstId();
auto info = fHnManager->GetHnInformation(id,"WriteOnAscii");
// skip writing if activation is enabled and H1 is inactivated
if ( ! info->GetAscii() ) continue;
auto h3 = fTVector[i];
Message(kVL3, "write on ascii", "h3d", info->GetName());
output << "\n 3D histogram " << id << ": " << h3->title()
<< "\n \n \t \t \t X \t\t Y \t\t Z \t\t Bin Height" << G4endl;
for (G4int j=0; j< G4int(h3->axis_x().bins()); ++j) {
for (G4int k=0; k< G4int(h3->axis_y().bins()); ++k) {
for (G4int l=0; l< G4int(h3->axis_y().bins()); ++l) {
output << " " << j << "\t" << k << "\t" << l << "\t"
<< h3->axis_x().bin_center(j) << "\t"
<< h3->axis_y().bin_center(k) << "\t"
<< h3->axis_y().bin_center(l) << "\t"
<< h3->bin_height(j, k, l) << G4endl;
}
}
}
}
return output.good();
}
//
// public methods
//
//_____________________________________________________________________________
G4int G4H3ToolsManager::AddH3(const G4String& name, tools::histo::h3d* h3d)
{
Message(kVL4, "add", "H3", name);
// Add annotation
AddH3Annotation(h3d, "none", "none", "none", "none", "none", "none");
// Add information
AddH3Information(name, "none", "none", "none", "none", "none", "none",
G4BinScheme::kLinear, G4BinScheme::kLinear, G4BinScheme::kLinear);
// Register histogram
G4int id = RegisterT(h3d, name);
Message(kVL2, "add", "H3", name);
return id;
}
//_____________________________________________________________________________
void G4H3ToolsManager::AddH3Vector(
const std::vector<tools::histo::h3d*>& h3Vector)
{
AddTVector(h3Vector);
}
//_____________________________________________________________________________
tools::histo::h3d* G4H3ToolsManager::GetH3(G4int id, G4bool warn,
G4bool onlyIfActive) const
{
return GetTInFunction(id, "GetH3", warn, onlyIfActive);
}
@@ -1,597 +0,0 @@
//
// ********************************************************************
// * 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, 24/07/2014 (ivana@ipno.in2p3.fr)
#include "G4P1ToolsManager.hh"
#include "G4AnalysisManagerState.hh"
#include "G4BaseHistoUtilities.hh"
#include "G4AnalysisUtilities.hh"
#include "tools/histo/p1d"
#include <fstream>
using namespace G4Analysis;
using std::to_string;
//
// Constructors, destructor
//
//_____________________________________________________________________________
G4P1ToolsManager::G4P1ToolsManager(const G4AnalysisManagerState& state)
: G4VP1Manager(),
G4THnManager<tools::histo::p1d>(state, "P1")
{}
//
// Utility functions
//
namespace {
//_____________________________________________________________________________
void UpdateP1Information(G4HnInformation* hnInformation,
const G4String& xunitName,
const G4String& yunitName,
const G4String& xfcnName,
const G4String& yfcnName,
G4BinScheme xbinScheme)
{
hnInformation->SetDimension(kX, xunitName, xfcnName, xbinScheme);
hnInformation->SetDimension(kY, yunitName, yfcnName, G4BinScheme::kLinear);
}
//_____________________________________________________________________________
void AddP1Annotation(tools::histo::p1d* p1d,
const G4String& xunitName,
const G4String& yunitName,
const G4String& xfcnName,
const G4String& yfcnName)
{
G4String xaxisTitle;
G4String yaxisTitle;
UpdateTitle(xaxisTitle, xunitName, xfcnName);
UpdateTitle(yaxisTitle, yunitName, yfcnName);
p1d->add_annotation(tools::histo::key_axis_x_title(), xaxisTitle);
p1d->add_annotation(tools::histo::key_axis_y_title(), yaxisTitle);
}
//_____________________________________________________________________________
tools::histo::p1d* CreateToolsP1(const G4String& title,
G4int nbins, G4double xmin, G4double xmax,
G4double ymin, G4double ymax,
const G4String& xunitName,
const G4String& yunitName,
const G4String& xfcnName,
const G4String& yfcnName,
const G4String& xbinSchemeName,
std::string_view className)
{
auto xunit = GetUnitValue(xunitName);
auto yunit = GetUnitValue(yunitName);
auto xfcn = GetFunction(xfcnName);
auto yfcn = GetFunction(yfcnName);
auto xbinScheme = GetBinScheme(xbinSchemeName);
if ( xbinScheme != G4BinScheme::kLog ) {
if ( xbinScheme == G4BinScheme::kUser ) {
// This should never happen, but let's make sure about it
// by issuing a warning
Warn("User binning scheme setting was ignored.\n"
"Linear binning will be applied with given (nbins, xmin, xmax) values.",
className, "CreateToolsP1");
}
if ( ymin == 0. && ymax == 0.) {
return new tools::histo::p1d(title,
nbins, xfcn(xmin/xunit), xfcn(xmax/xunit));
} else {
return new tools::histo::p1d(title,
nbins, xfcn(xmin/xunit), xfcn(xmax/xunit),
yfcn(ymin/yunit), yfcn(ymax/yunit));
}
}
else {
// Compute edges
std::vector<G4double> edges;
ComputeEdges(nbins, xmin, xmax, xunit, xfcn, xbinScheme, edges);
if ( ymin == 0. && ymax == 0.) {
return new tools::histo::p1d(title, edges);
} else {
return new tools::histo::p1d(title, edges, yfcn(ymin/yunit), yfcn(ymax/yunit));
}
}
}
//_____________________________________________________________________________
tools::histo::p1d* CreateToolsP1(const G4String& title,
const std::vector<G4double>& edges,
G4double ymin, G4double ymax,
const G4String& xunitName,
const G4String& yunitName,
const G4String& xfcnName,
const G4String& yfcnName)
{
auto xunit = GetUnitValue(xunitName);
auto yunit = GetUnitValue(yunitName);
auto xfcn = GetFunction(xfcnName);
auto yfcn = GetFunction(yfcnName);
// Apply function
std::vector<G4double> newEdges;
ComputeEdges(edges, xunit, xfcn, newEdges);
return new tools::histo::p1d(title, newEdges, yfcn(ymin/yunit), yfcn(ymax/yunit));
}
//_____________________________________________________________________________
void ConfigureToolsP1(tools::histo::p1d* p1d,
G4int nbins, G4double xmin, G4double xmax,
G4double ymin, G4double ymax,
const G4String& xunitName,
const G4String& yunitName,
const G4String& xfcnName,
const G4String& yfcnName,
const G4String& xbinSchemeName,
std::string_view className)
{
auto xunit = GetUnitValue(xunitName);
auto yunit = GetUnitValue(yunitName);
auto xfcn = GetFunction(xfcnName);
auto yfcn = GetFunction(yfcnName);
auto xbinScheme = GetBinScheme(xbinSchemeName);
if ( xbinScheme != G4BinScheme::kLog ) {
if ( xbinScheme == G4BinScheme::kUser ) {
// This should never happen, but let's make sure about it
// by issuing a warning
Warn("User binning scheme setting was ignored.\n"
"Linear binning will be applied with given (nbins, xmin, xmax) values.",
className, "ConfigureToolsP1");
}
if ( ymin == 0. && ymax == 0. ) {
p1d->configure(nbins, xfcn(xmin/xunit), xfcn(xmax/xunit));
} else {
p1d->configure(nbins, xfcn(xmin/xunit), xfcn(xmax/xunit),
yfcn(ymin/yunit), yfcn(ymax/yunit));
}
}
else {
// Compute bins
std::vector<G4double> edges;
ComputeEdges(nbins, xmin, xmax, xunit, xfcn, xbinScheme, edges);
if ( ymin == 0. && ymax == 0. ) {
p1d->configure(edges);
} else {
p1d->configure(edges, yfcn(ymin/yunit), yfcn(ymax/yunit));
}
}
}
//_____________________________________________________________________________
void ConfigureToolsP1(tools::histo::p1d* p1d,
const std::vector<G4double>& edges,
G4double ymin, G4double ymax,
const G4String& xunitName,
const G4String& yunitName,
const G4String& xfcnName,
const G4String& yfcnName)
{
// Apply function to edges
auto xunit = GetUnitValue(xunitName);
auto yunit = GetUnitValue(yunitName);
auto xfcn = GetFunction(xfcnName);
auto yfcn = GetFunction(yfcnName);
std::vector<G4double> newEdges;
ComputeEdges(edges, xunit, xfcn, newEdges);
if ( ymin == 0. && ymax == 0. ) {
p1d->configure(newEdges);
} else {
p1d->configure(newEdges, yfcn(ymin/yunit), yfcn(ymax/yunit));
}
}
}
//
// private methods
//
//_____________________________________________________________________________
void G4P1ToolsManager::AddP1Information(const G4String& name,
const G4String& xunitName,
const G4String& yunitName,
const G4String& xfcnName,
const G4String& yfcnName,
G4BinScheme xbinScheme) const
{
auto hnInformation = fHnManager->AddHnInformation(name, fkDimension);
hnInformation->AddDimension(xunitName, xfcnName, xbinScheme);
hnInformation->AddDimension(yunitName, yfcnName, G4BinScheme::kLinear);
}
//
// protected methods
//
//_____________________________________________________________________________
G4int G4P1ToolsManager::CreateP1(const G4String& name, const G4String& title,
G4int nbins, G4double xmin, G4double xmax,
G4double ymin, G4double ymax,
const G4String& xunitName, const G4String& yunitName,
const G4String& xfcnName, const G4String& yfcnName,
const G4String& xbinSchemeName)
{
Message(kVL4, "create", "P1", name);
tools::histo::p1d* p1d
= CreateToolsP1(title, nbins, xmin, xmax, ymin, ymax,
xunitName, yunitName, xfcnName, yfcnName,
xbinSchemeName, fkClass);
// Add annotation
AddP1Annotation(p1d, xunitName, yunitName, xfcnName, yfcnName);
// Save P1 information
auto xbinScheme = GetBinScheme(xbinSchemeName);
AddP1Information(
name, xunitName, yunitName, xfcnName, yfcnName, xbinScheme);
// Register profile
G4int id = RegisterT(p1d, name);
Message(kVL2, "create", "P1", name);
return id;
}
//_____________________________________________________________________________
G4int G4P1ToolsManager::CreateP1(const G4String& name, const G4String& title,
const std::vector<G4double>& edges,
G4double ymin, G4double ymax,
const G4String& xunitName, const G4String& yunitName,
const G4String& xfcnName, const G4String& yfcnName)
{
Message(kVL4, "create", "P1", name);
tools::histo::p1d* p1d
= CreateToolsP1(title, edges, ymin, ymax,
xunitName, yunitName, xfcnName, yfcnName);
// Add annotation
AddP1Annotation(p1d, xunitName, yunitName, xfcnName, yfcnName);
// Save P1 information
AddP1Information(
name, xunitName, yunitName, xfcnName, yfcnName, G4BinScheme::kUser);
// Register profile
G4int id = RegisterT(p1d, name);
Message(kVL2, "create", "P1", name);
return id;
}
//_____________________________________________________________________________
G4bool G4P1ToolsManager::SetP1(G4int id,
G4int nbins, G4double xmin, G4double xmax,
G4double ymin, G4double ymax,
const G4String& xunitName, const G4String& yunitName,
const G4String& xfcnName, const G4String& yfcnName,
const G4String& xbinSchemeName)
{
auto p1d = GetTInFunction(id, "SetP1", false, false);
if ( ! p1d ) return false;
auto info = fHnManager->GetHnInformation(id,"SetP1");
Message(kVL4, "configure", "P1", info->GetName());
// Configure tools p1
ConfigureToolsP1(
p1d, nbins, xmin, xmax, ymin, ymax,
xunitName, yunitName, xfcnName, yfcnName, xbinSchemeName, fkClass);
// Add annotation
AddP1Annotation(p1d, xunitName, yunitName, xfcnName, yfcnName);
// Update information
auto xbinScheme = GetBinScheme(xbinSchemeName);
UpdateP1Information(
info, xunitName, yunitName, xfcnName, yfcnName, xbinScheme);
// Set activation
fHnManager->SetActivation(id, true);
return true;
}
//_____________________________________________________________________________
G4bool G4P1ToolsManager::SetP1(G4int id,
const std::vector<G4double>& edges,
G4double ymin, G4double ymax,
const G4String& xunitName, const G4String& yunitName,
const G4String& xfcnName, const G4String& yfcnName)
{
auto p1d = GetTInFunction(id, "SetP1", false, false);
if ( ! p1d ) return false;
auto info = fHnManager->GetHnInformation(id,"SetP1");
Message(kVL4, "configure", "P1", info->GetName());
// Configure tools p1
ConfigureToolsP1(p1d, edges, ymin, ymax,
xunitName, yunitName, xfcnName, yfcnName);
// Add annotation
AddP1Annotation(p1d, xunitName, yunitName, xfcnName, yfcnName);
// Update information
UpdateP1Information(
info, xunitName, yunitName, xfcnName, yfcnName, G4BinScheme::kUser);
// Set activation
fHnManager->SetActivation(id, true);
return true;
}
//_____________________________________________________________________________
G4bool G4P1ToolsManager::ScaleP1(G4int id, G4double factor)
{
auto p1d = GetTInFunction(id, "ScaleP1", false, false);
if ( ! p1d ) return false;
return p1d->scale(factor);
}
//_____________________________________________________________________________
G4bool G4P1ToolsManager::FillP1(G4int id, G4double xvalue, G4double yvalue,
G4double weight)
{
auto p1d = GetTInFunction(id, "FillP1", true, false);
if ( ! p1d ) return false;
if ( fState.GetIsActivation() && ( ! fHnManager->GetActivation(id) ) ) {
//G4cout << "Skipping FillP1 for " << id << G4endl;
return false;
}
auto xInfo
= fHnManager->GetHnDimensionInformation(id, kX, "FillP1");
auto yInfo
= fHnManager->GetHnDimensionInformation(id, kY, "FillP1");
p1d->fill(xInfo->fFcn(xvalue/xInfo->fUnit),
yInfo->fFcn(yvalue/yInfo->fUnit), weight);
if ( IsVerbose(kVL4) ) {
Message(kVL4, "fill", "P1",
" id " + to_string(id) +
" xvalue " + to_string(xvalue) +
" xfcn(xvalue/xunit) " + to_string(xInfo->fFcn(xvalue/xInfo->fUnit)) +
" yvalue " + to_string(yvalue) +
" yfcn(yvalue/yunit) " + to_string(yInfo->fFcn(yvalue/yInfo->fUnit)) +
" weight " + to_string(weight));
}
return true;
}
//_____________________________________________________________________________
G4int G4P1ToolsManager::GetP1Id(const G4String& name, G4bool warn) const
{
return GetTId(name, warn);
}
//_____________________________________________________________________________
G4int G4P1ToolsManager::GetP1Nbins(G4int id) const
{
auto p1d = GetTInFunction(id, "GetP1Nbins");
if ( ! p1d ) return 0;
return GetNbins(*p1d, kX);
}
//_____________________________________________________________________________
G4double G4P1ToolsManager::GetP1Xmin(G4int id) const
{
// Returns xmin value with applied unit and profile function
auto p1d = GetTInFunction(id, "GetP1Xmin");
if ( ! p1d ) return 0.;
return GetMin(*p1d, kX);
}
//_____________________________________________________________________________
G4double G4P1ToolsManager::GetP1Xmax(G4int id) const
{
auto p1d = GetTInFunction(id, "GetP1Xmax");
if ( ! p1d ) return 0.;
return GetMax(*p1d, kX);
}
//_____________________________________________________________________________
G4double G4P1ToolsManager::GetP1XWidth(G4int id) const
{
auto p1d = GetTInFunction(id, "GetP1XWidth", true, false);
if ( ! p1d ) return 0.;
return GetWidth(*p1d, kX, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4double G4P1ToolsManager::GetP1Ymin(G4int id) const
{
// Returns xmin value with applied unit and profile function
auto p1d = GetTInFunction(id, "GetP1Ymin");
if ( ! p1d ) return 0.;
return p1d->min_v();
}
//_____________________________________________________________________________
G4double G4P1ToolsManager::GetP1Ymax(G4int id) const
{
auto p1d = GetTInFunction(id, "GetP1Ymax");
if ( ! p1d ) return 0.;
return p1d->max_v();
}
//_____________________________________________________________________________
G4bool G4P1ToolsManager::SetP1Title(G4int id, const G4String& title)
{
auto p1d = GetTInFunction(id, "SetP1Title");
if ( ! p1d ) return false;
return SetTitle(*p1d, title);
}
//_____________________________________________________________________________
G4bool G4P1ToolsManager::SetP1XAxisTitle(G4int id, const G4String& title)
{
auto p1d = GetTInFunction(id, "SetP1XAxisTitle");
if ( ! p1d ) return false;
return SetAxisTitle(*p1d, kX, title);
}
//_____________________________________________________________________________
G4bool G4P1ToolsManager::SetP1YAxisTitle(G4int id, const G4String& title)
{
auto p1d = GetTInFunction(id, "SetP1YAxisTitle");
if ( ! p1d ) return false;
return SetAxisTitle(*p1d, kY, title);
}
//_____________________________________________________________________________
G4String G4P1ToolsManager::GetP1Title(G4int id) const
{
auto p1d = GetTInFunction(id, "GetP1Title");
if ( ! p1d ) return "";
return GetTitle(*p1d);
}
//_____________________________________________________________________________
G4String G4P1ToolsManager::GetP1XAxisTitle(G4int id) const
{
auto p1d = GetTInFunction(id, "GetP1XAxisTitle");
if ( ! p1d ) return "";
return GetAxisTitle(*p1d, kX, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4String G4P1ToolsManager::GetP1YAxisTitle(G4int id) const
{
auto p1d = GetTInFunction(id, "GetP1YAxisTitle");
if ( ! p1d ) return "";
return GetAxisTitle(*p1d, kY, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4bool G4P1ToolsManager::WriteOnAscii(std::ofstream& output)
{
// Write selected objects on ASCII file
// Do nothing if no histograms are selected
if ( ! fHnManager->IsAscii() ) return true;
// Write p1 histograms
for ( G4int i=0; i<G4int(fTVector.size()); ++i ) {
auto id = i + fHnManager->GetFirstId();
auto info = fHnManager->GetHnInformation(id,"WriteOnAscii");
// skip writing if activation is enabled and H1 is inactivated
if ( ! info->GetAscii() ) continue;
auto p1 = fTVector[i];
Message(kVL3, "write on ascii", "p1d", info->GetName());
output << "\n 1D profile " << id << ": " << p1->title()
<< "\n \n \t X \t\t MeanY" << G4endl;
for (G4int j=0; j< G4int(p1->axis().bins()); ++j) {
auto sw = p1->bin_Sw(j);
auto svw = p1->bin_Svw(j);
auto mean = ( sw != 0. ) ? (svw / sw) : 0.;
output << " " << j << "\t"
<< p1->axis().bin_center(j) << "\t"
<< mean << G4endl;
}
}
return output.good();
}
//
// public methods
//
//_____________________________________________________________________________
G4int G4P1ToolsManager::AddP1(const G4String& name, tools::histo::p1d* p1d)
{
Message(kVL4, "add", "P1", name);
// Add annotation
AddP1Annotation(p1d, "none", "none", "none", "none");
// Add information
AddP1Information(name, "none", "none", "none", "none", G4BinScheme::kLinear);
// Register profile
auto id = RegisterT(p1d, name);
Message(kVL2, "add", "P1", name);
return id;
}
//_____________________________________________________________________________
void G4P1ToolsManager::AddP1Vector(
const std::vector<tools::histo::p1d*>& p1Vector)
{
AddTVector(p1Vector);
}
//_____________________________________________________________________________
tools::histo::p1d* G4P1ToolsManager::GetP1(G4int id, G4bool warn,
G4bool onlyIfActive) const
{
return GetTInFunction(id, "GetP1", warn, onlyIfActive);
}
@@ -1,752 +0,0 @@
//
// ********************************************************************
// * 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, 24/07/2014 (ivana@ipno.in2p3.fr)
#include "G4P2ToolsManager.hh"
#include "G4AnalysisManagerState.hh"
#include "G4BaseHistoUtilities.hh"
#include "G4AnalysisUtilities.hh"
#include "tools/histo/p2d"
#include <fstream>
using namespace G4Analysis;
using std::to_string;
//_____________________________________________________________________________
G4P2ToolsManager::G4P2ToolsManager(const G4AnalysisManagerState& state)
: G4VP2Manager(),
G4THnManager<tools::histo::p2d>(state, "P2")
{}
//
// Utility functions
//
namespace {
//_____________________________________________________________________________
void UpdateP2Information(G4HnInformation* hnInformation,
const G4String& xunitName,
const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName,
const G4String& yfcnName,
const G4String& zfcnName,
G4BinScheme xbinScheme,
G4BinScheme ybinScheme)
{
hnInformation->SetDimension(kX, xunitName, xfcnName, xbinScheme);
hnInformation->SetDimension(kY, yunitName, yfcnName, ybinScheme);
hnInformation->SetDimension(kZ, zunitName, zfcnName, G4BinScheme::kLinear);
}
//_____________________________________________________________________________
void AddP2Annotation(tools::histo::p2d* p2d,
const G4String& xunitName,
const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName,
const G4String& yfcnName,
const G4String& zfcnName)
{
G4String xaxisTitle;
G4String yaxisTitle;
G4String zaxisTitle;
UpdateTitle(xaxisTitle, xunitName, xfcnName);
UpdateTitle(yaxisTitle, yunitName, yfcnName);
UpdateTitle(zaxisTitle, zunitName, zfcnName);
p2d->add_annotation(tools::histo::key_axis_x_title(), xaxisTitle);
p2d->add_annotation(tools::histo::key_axis_y_title(), yaxisTitle);
p2d->add_annotation(tools::histo::key_axis_z_title(), zaxisTitle);
}
//_____________________________________________________________________________
tools::histo::p2d* CreateToolsP2(
const G4String& title,
G4int nxbins, G4double xmin, G4double xmax,
G4int nybins, G4double ymin, G4double ymax,
G4double zmin, G4double zmax,
const G4String& xunitName,
const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName,
const G4String& zfcnName,
const G4String& yfcnName,
const G4String& xbinSchemeName,
const G4String& ybinSchemeName,
std::string_view className)
{
auto xunit = GetUnitValue(xunitName);
auto yunit = GetUnitValue(yunitName);
auto zunit = GetUnitValue(zunitName);
auto xfcn = GetFunction(xfcnName);
auto yfcn = GetFunction(yfcnName);
auto zfcn = GetFunction(zfcnName);
auto xbinScheme = GetBinScheme(xbinSchemeName);
auto ybinScheme = GetBinScheme(ybinSchemeName);
if ( xbinScheme != G4BinScheme::kLog && ybinScheme != G4BinScheme::kLog) {
if ( xbinScheme == G4BinScheme::kUser || ybinScheme == G4BinScheme::kUser ) {
// This should never happen, but let's make sure about it
// by issuing a warning
Warn("User binning scheme setting was ignored.\n"
"Linear binning will be applied with given (nbins, xmin, xmax) values.",
className, "CreateToolsP2");
}
if ( zmin == 0. && zmax == 0.) {
return new tools::histo::p2d(title,
nxbins, xfcn(xmin/xunit), xfcn(xmax/xunit),
nybins, yfcn(ymin/yunit), yfcn(ymax/yunit));
// p2 objects are deleted in destructor and reset when
// closing a file.
} else {
return new tools::histo::p2d(title,
nxbins, xfcn(xmin/xunit), xfcn(xmax/xunit),
nybins, yfcn(ymin/yunit), yfcn(ymax/yunit),
zfcn(zmin/zunit), zfcn(zmax/zunit));
// p2 objects are deleted in destructor and reset when
// closing a file.
}
}
else {
// Compute edges
std::vector<G4double> xedges;
ComputeEdges(nxbins, xmin, xmax, xunit, xfcn, xbinScheme, xedges);
std::vector<G4double> yedges;
ComputeEdges(nybins, ymin, ymax, yunit, yfcn, ybinScheme, yedges);
if ( zmin == 0. && zmax == 0.) {
return new tools::histo::p2d(title, xedges, yedges);
} else {
return new tools::histo::p2d(title, xedges, yedges,
zfcn(zmin/zunit), zfcn(zmax/zunit));
}
}
}
//_____________________________________________________________________________
tools::histo::p2d* CreateToolsP2(
const G4String& title,
const std::vector<G4double>& xedges,
const std::vector<G4double>& yedges,
G4double zmin, G4double zmax,
const G4String& xunitName,
const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName,
const G4String& yfcnName,
const G4String& zfcnName)
{
auto xunit = GetUnitValue(xunitName);
auto yunit = GetUnitValue(yunitName);
auto zunit = GetUnitValue(zunitName);
auto xfcn = GetFunction(xfcnName);
auto yfcn = GetFunction(yfcnName);
auto zfcn = GetFunction(zfcnName);
// Apply function
std::vector<G4double> xnewEdges;
ComputeEdges(xedges, xunit, xfcn, xnewEdges);
std::vector<G4double> ynewEdges;
ComputeEdges(yedges, yunit, yfcn, ynewEdges);
if ( zmin == 0. && zmax == 0.) {
return new tools::histo::p2d(title, xnewEdges, ynewEdges);
// p2 objects are deleted in destructor and reset when
// closing a file.
} else {
return new tools::histo::p2d(title, xnewEdges, ynewEdges,
zfcn(zmin/zunit), zfcn(zmax/zunit));
// p2 objects are deleted in destructor and reset when
// closing a file.
}
}
//_____________________________________________________________________________
void ConfigureToolsP2(tools::histo::p2d* p2d,
G4int nxbins, G4double xmin, G4double xmax,
G4int nybins, G4double ymin, G4double ymax,
G4double zmin, G4double zmax,
const G4String& xunitName,
const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName,
const G4String& yfcnName,
const G4String& zfcnName,
const G4String& xbinSchemeName,
const G4String& ybinSchemeName,
std::string_view className)
{
auto xunit = GetUnitValue(xunitName);
auto yunit = GetUnitValue(yunitName);
auto zunit = GetUnitValue(zunitName);
auto xfcn = GetFunction(xfcnName);
auto yfcn = GetFunction(yfcnName);
auto zfcn = GetFunction(zfcnName);
auto xbinScheme = GetBinScheme(xbinSchemeName);
auto ybinScheme = GetBinScheme(ybinSchemeName);
if ( xbinScheme != G4BinScheme::kLog && ybinScheme != G4BinScheme::kLog) {
if ( xbinScheme == G4BinScheme::kUser || ybinScheme == G4BinScheme::kUser) {
// This should never happen, but let's make sure about it
// by issuing a warning
Warn("User binning scheme setting was ignored.\n"
"Linear binning will be applied with given (nbins, xmin, xmax) values.",
className, "ConfigureToolsP2");
}
if ( zmin == 0. && zmax == 0. ) {
p2d->configure(nxbins, xfcn(xmin/xunit), xfcn(xmax/xunit),
nybins, yfcn(ymin/yunit), yfcn(ymax/yunit));
} else {
p2d->configure(nxbins, xfcn(xmin/xunit), xfcn(xmax/xunit),
nybins, yfcn(ymin/yunit), yfcn(ymax/yunit),
zfcn(zmin/zunit), zfcn(zmax/zunit));
}
}
else {
// Compute bins
std::vector<G4double> xedges;
ComputeEdges(nxbins, xmin, xmax, xunit, xfcn, xbinScheme, xedges);
std::vector<G4double> yedges;
ComputeEdges(nybins, ymin, ymax, yunit, yfcn, ybinScheme, yedges);
if ( zmin == 0. && zmax == 0. ) {
p2d->configure(xedges, yedges);
} else {
p2d->configure(xedges, yedges, zfcn(zmin/zunit), zfcn(zmax/zunit));
}
}
}
//_____________________________________________________________________________
void ConfigureToolsP2(tools::histo::p2d* p2d,
const std::vector<G4double>& xedges,
const std::vector<G4double>& yedges,
G4double zmin, G4double zmax,
const G4String& xunitName,
const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName,
const G4String& yfcnName,
const G4String& zfcnName)
{
// Apply function to edges
auto xunit = GetUnitValue(xunitName);
auto xfcn = GetFunction(xfcnName);
std::vector<G4double> xnewEdges;
ComputeEdges(xedges, xunit, xfcn, xnewEdges);
auto yunit = GetUnitValue(yunitName);
auto yfcn = GetFunction(yfcnName);
std::vector<G4double> ynewEdges;
ComputeEdges(yedges, yunit, yfcn, ynewEdges);
auto zunit = GetUnitValue(zunitName);
auto zfcn = GetFunction(zfcnName);
if ( zmin == 0. && zmax == 0. ) {
p2d->configure(xnewEdges, ynewEdges);
} else {
p2d->configure(xnewEdges, ynewEdges, zfcn(zmin/zunit), zfcn(zmax/zunit));
}
}
}
//
// private methods
//
//_____________________________________________________________________________
void G4P2ToolsManager::AddP2Information(const G4String& name,
const G4String& xunitName,
const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName,
const G4String& yfcnName,
const G4String& zfcnName,
G4BinScheme xbinScheme,
G4BinScheme ybinScheme) const
{
auto hnInformation = fHnManager->AddHnInformation(name, fkDimension);
hnInformation->AddDimension(xunitName, xfcnName, xbinScheme);
hnInformation->AddDimension(yunitName, yfcnName, ybinScheme);
hnInformation->AddDimension(zunitName, zfcnName, G4BinScheme::kLinear);
}
//
// protected methods
//
//_____________________________________________________________________________
G4int G4P2ToolsManager::CreateP2(const G4String& name, const G4String& title,
G4int nxbins, G4double xmin, G4double xmax,
G4int nybins, G4double ymin, G4double ymax,
G4double zmin, G4double zmax,
const G4String& xunitName, const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName, const G4String& yfcnName,
const G4String& zfcnName,
const G4String& xbinSchemeName,
const G4String& ybinSchemeName)
{
Message(kVL4, "create", "P2", name);
tools::histo::p2d* p2d
= CreateToolsP2(title,
nxbins, xmin, xmax, nybins, ymin, ymax, zmin, zmax,
xunitName, yunitName, zunitName,
xfcnName, yfcnName, zfcnName,
xbinSchemeName, ybinSchemeName, fkClass);
// Add annotation
AddP2Annotation(p2d, xunitName, yunitName, zunitName,
xfcnName, yfcnName, zfcnName);
// Save P2 information
auto xbinScheme = GetBinScheme(xbinSchemeName);
auto ybinScheme = GetBinScheme(ybinSchemeName);
AddP2Information(
name, xunitName, yunitName, zunitName, xfcnName, yfcnName, zfcnName,
xbinScheme, ybinScheme);
// Register profile
G4int id = RegisterT(p2d, name);
Message(kVL2, "create", "P2", name);
return id;
}
//_____________________________________________________________________________
G4int G4P2ToolsManager::CreateP2(const G4String& name, const G4String& title,
const std::vector<G4double>& xedges,
const std::vector<G4double>& yedges,
G4double zmin, G4double zmax,
const G4String& xunitName, const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName, const G4String& yfcnName,
const G4String& zfcnName)
{
Message(kVL4, "create", "P2", name);
tools::histo::p2d* p2d
= CreateToolsP2(title, xedges, yedges, zmin, zmax,
xunitName, yunitName, zunitName, xfcnName, yfcnName, zfcnName);
// Add annotation
AddP2Annotation(
p2d, xunitName, yunitName, zunitName, xfcnName, yfcnName, zfcnName);
// Save P2 information
AddP2Information(
name, xunitName, yunitName, zunitName, xfcnName, yfcnName, zfcnName,
G4BinScheme::kUser, G4BinScheme::kUser);
// Register profile
G4int id = RegisterT(p2d, name);
Message(kVL2, "create", "P2", name);
return id;
}
//_____________________________________________________________________________
G4bool G4P2ToolsManager::SetP2(G4int id,
G4int nxbins, G4double xmin, G4double xmax,
G4int nybins, G4double ymin, G4double ymax,
G4double zmin, G4double zmax,
const G4String& xunitName, const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName, const G4String& yfcnName,
const G4String& zfcnName,
const G4String& xbinSchemeName,
const G4String& ybinSchemeName)
{
auto p2d = GetTInFunction(id, "SetP2", false, false);
if ( ! p2d ) return false;
auto info = fHnManager->GetHnInformation(id, "SetP2");
Message(kVL4, "configure", "P2", info->GetName());
// Configure tools p2
ConfigureToolsP2(
p2d, nxbins, xmin, xmax, nybins, ymin, ymax, zmin, zmax,
xunitName, yunitName, zunitName, xfcnName, yfcnName, zfcnName,
xbinSchemeName, ybinSchemeName, fkClass);
// Add annotation
AddP2Annotation(p2d, xunitName, yunitName, zunitName,
xfcnName, yfcnName, zfcnName);
// Update information
auto xbinScheme = GetBinScheme(xbinSchemeName);
auto ybinScheme = GetBinScheme(ybinSchemeName);
UpdateP2Information(
info, xunitName, yunitName, zunitName, xfcnName, yfcnName, zfcnName,
xbinScheme, ybinScheme);
// Set activation
fHnManager->SetActivation(id, true);
return true;
}
//_____________________________________________________________________________
G4bool G4P2ToolsManager::SetP2(G4int id,
const std::vector<G4double>& xedges,
const std::vector<G4double>& yedges,
G4double zmin, G4double zmax,
const G4String& xunitName, const G4String& yunitName,
const G4String& zunitName,
const G4String& xfcnName, const G4String& yfcnName,
const G4String& zfcnName)
{
auto p2d = GetTInFunction(id, "SetP2", false, false);
if ( ! p2d ) return false;
auto info = fHnManager->GetHnInformation(id, "SetP2");
Message(kVL4, "configure", "P2", info->GetName());
// Configure tools p2
ConfigureToolsP2(p2d, xedges, yedges, zmin, zmax,
xunitName, yunitName, zunitName, xfcnName, yfcnName, zfcnName);
// Add annotation
AddP2Annotation(p2d, xunitName, yunitName, zunitName,
xfcnName, yfcnName, zfcnName);
// Update information
UpdateP2Information(
info, xunitName, yunitName, zunitName, xfcnName, yfcnName, zfcnName,
G4BinScheme::kUser, G4BinScheme::kUser);
// Set activation
fHnManager->SetActivation(id, true);
return true;
}
//_____________________________________________________________________________
G4bool G4P2ToolsManager::ScaleP2(G4int id, G4double factor)
{
auto p2d = GetTInFunction(id, "ScaleP2", false, false);
if ( ! p2d ) return false;
return p2d->scale(factor);
}
//_____________________________________________________________________________
G4bool G4P2ToolsManager::FillP2(G4int id,
G4double xvalue, G4double yvalue, G4double zvalue,
G4double weight)
{
auto p2d = GetTInFunction(id, "FillP2", true, false);
if ( ! p2d ) return false;
if ( fState.GetIsActivation() && ( ! fHnManager->GetActivation(id) ) ) {
return false;
}
auto xInfo
= fHnManager->GetHnDimensionInformation(id, kX, "FillP2");
auto yInfo
= fHnManager->GetHnDimensionInformation(id, kY, "FillP2");
auto zInfo
= fHnManager->GetHnDimensionInformation(id, kZ, "FillP2");
p2d->fill(xInfo->fFcn(xvalue/xInfo->fUnit),
yInfo->fFcn(yvalue/yInfo->fUnit),
zInfo->fFcn(zvalue/zInfo->fUnit), weight);
if ( IsVerbose(kVL4) ) {
Message(kVL4, "fill", "P2",
" id " + to_string(id) +
" xvalue " + to_string(xvalue) +
" xfcn(xvalue/xunit) " + to_string(xInfo->fFcn(xvalue/xInfo->fUnit)) +
" yvalue " + to_string(yvalue) +
" yfcn(yvalue/yunit) " + to_string(yInfo->fFcn(yvalue/yInfo->fUnit)) +
" zvalue " + to_string(zvalue) +
" zfcn(zvalue/zunit) " + to_string(zInfo->fFcn(zvalue/zInfo->fUnit)) +
" weight " + to_string(weight));
}
return true;
}
//_____________________________________________________________________________
G4int G4P2ToolsManager::GetP2Id(const G4String& name, G4bool warn) const
{
return GetTId(name, warn);
}
//_____________________________________________________________________________
G4int G4P2ToolsManager::GetP2Nxbins(G4int id) const
{
auto p2d = GetTInFunction(id, "GetP2NXbins");
if ( ! p2d ) return 0;
return GetNbins(*p2d, kX);
}
//_____________________________________________________________________________
G4double G4P2ToolsManager::GetP2Xmin(G4int id) const
{
// Returns xmin value with applied unit and profile function
auto p2d = GetTInFunction(id, "GetP2Xmin");
if ( ! p2d ) return 0.;
return GetMin(*p2d, kX);
}
//_____________________________________________________________________________
G4double G4P2ToolsManager::GetP2Xmax(G4int id) const
{
auto p2d = GetTInFunction(id, "GetP2Xmax");
if ( ! p2d ) return 0.;
return GetMax(*p2d, kX);
}
//_____________________________________________________________________________
G4double G4P2ToolsManager::GetP2XWidth(G4int id) const
{
auto p2d = GetTInFunction(id, "GetP2XWidth", true, false);
if ( ! p2d ) return 0.;
return GetWidth(*p2d, kX, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4int G4P2ToolsManager::GetP2Nybins(G4int id) const
{
auto p2d = GetTInFunction(id, "GetP2NYbins");
if ( ! p2d ) return 0;
return GetNbins(*p2d, kY);
}
//_____________________________________________________________________________
G4double G4P2ToolsManager::GetP2Ymin(G4int id) const
{
// Returns xmin value with applied unit and profile function
auto p2d = GetTInFunction(id, "GetP2Ymin");
if ( ! p2d ) return 0.;
return GetMin(*p2d, kY);
}
//_____________________________________________________________________________
G4double G4P2ToolsManager::GetP2Ymax(G4int id) const
{
auto p2d = GetTInFunction(id, "GetP2Ymax");
if ( ! p2d ) return 0.;
return GetMax(*p2d, kY);
}
//_____________________________________________________________________________
G4double G4P2ToolsManager::GetP2YWidth(G4int id) const
{
auto p2d = GetTInFunction(id, "GetP2YWidth", true, false);
if ( ! p2d ) return 0.;
return GetWidth(*p2d, kY, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4double G4P2ToolsManager::GetP2Zmin(G4int id) const
{
// Returns xmin value with applied unit and profile function
auto p2d = GetTInFunction(id, "GetP2Zmin");
if ( ! p2d ) return 0.;
return GetMin(*p2d, kZ);
}
//_____________________________________________________________________________
G4double G4P2ToolsManager::GetP2Zmax(G4int id) const
{
auto p2d = GetTInFunction(id, "GetP2Zmax");
if ( ! p2d ) return 0.;
return GetMax(*p2d, kZ);
}
//_____________________________________________________________________________
G4bool G4P2ToolsManager::SetP2Title(G4int id, const G4String& title)
{
auto p2d = GetTInFunction(id, "SetP2Title");
if ( ! p2d ) return false;
return SetTitle(*p2d, title);
}
//_____________________________________________________________________________
G4bool G4P2ToolsManager::SetP2XAxisTitle(G4int id, const G4String& title)
{
auto p2d = GetTInFunction(id, "SetP2XAxisTitle");
if ( ! p2d ) return false;
return SetAxisTitle(*p2d, kX, title);
}
//_____________________________________________________________________________
G4bool G4P2ToolsManager::SetP2YAxisTitle(G4int id, const G4String& title)
{
auto p2d = GetTInFunction(id, "SetP2YAxisTitle");
if ( ! p2d ) return false;
return SetAxisTitle(*p2d, kY, title);
}
//_____________________________________________________________________________
G4bool G4P2ToolsManager::SetP2ZAxisTitle(G4int id, const G4String& title)
{
auto p2d = GetTInFunction(id, "SetP2ZAxisTitle");
if ( ! p2d ) return false;
return SetAxisTitle(*p2d, kZ, title);
}
//_____________________________________________________________________________
G4String G4P2ToolsManager::GetP2Title(G4int id) const
{
auto p2d = GetTInFunction(id, "GetP2Title");
if ( ! p2d ) return "";
return GetTitle(*p2d);
}
//_____________________________________________________________________________
G4String G4P2ToolsManager::GetP2XAxisTitle(G4int id) const
{
auto p2d = GetTInFunction(id, "GetP2XAxisTitle");
if ( ! p2d ) return "";
return GetAxisTitle(*p2d, kX, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4String G4P2ToolsManager::GetP2YAxisTitle(G4int id) const
{
auto p2d = GetTInFunction(id, "GetP2YAxisTitle");
if ( ! p2d ) return "";
return GetAxisTitle(*p2d, kY, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4String G4P2ToolsManager::GetP2ZAxisTitle(G4int id) const
{
auto p2d = GetTInFunction(id, "GetP2ZAxisTitle");
if ( ! p2d ) return "";
return GetAxisTitle(*p2d, kZ, fHnManager->GetHnType());
}
//_____________________________________________________________________________
G4bool G4P2ToolsManager::WriteOnAscii(std::ofstream& output)
{
// Write selected objects on ASCII file
// Do nothing if no histograms are selected
if ( ! fHnManager->IsAscii() ) return true;
// Write p2 histograms
for ( G4int i=0; i<G4int(fTVector.size()); ++i ) {
auto id = i + fHnManager->GetFirstId();
auto info = fHnManager->GetHnInformation(id,"WriteOnAscii");
// skip writing if activation is enabled and H1 is inactivated
if ( ! info->GetAscii() ) continue;
auto p2 = fTVector[i];
Message(kVL3, "write on ascii", "p2d", info->GetName());
output << "\n 2D profile " << id << ": " << p2->title()
<< "\n \n \t \t X \t\t Y \t\t MeanZ" << G4endl;
for (G4int j=0; j< G4int(p2->axis_x().bins()); ++j) {
for (G4int k=0; k< G4int(p2->axis_y().bins()); ++k) {
auto sw = p2->bin_Sw(j, k);
auto svw = p2->bin_Svw(j, k);
auto mean = ( sw != 0. ) ? (svw / sw) : 0.;
output << " " << j << "\t" << k << "\t"
<< p2->axis_x().bin_center(j) << "\t"
<< p2->axis_y().bin_center(k) << "\t"
<< mean << G4endl;
}
}
}
return output.good();
}
//
// public methods
//
//_____________________________________________________________________________
G4int G4P2ToolsManager::AddP2(const G4String& name, tools::histo::p2d* p2d)
{
Message(kVL4, "add", "P2", name);
// Add annotation
AddP2Annotation(p2d, "none", "none", "none", "none", "none", "none");
// Add information
AddP2Information(name, "none", "none", "none", "none", "none", "none",
G4BinScheme::kLinear, G4BinScheme::kLinear);
// Register profile
G4int id = RegisterT(p2d, name);
Message(kVL2, "add", "P2", name);
return id;
}
//_____________________________________________________________________________
void G4P2ToolsManager::AddP2Vector(
const std::vector<tools::histo::p2d*>& p2Vector)
{
AddTVector(p2Vector);
}
//_____________________________________________________________________________
tools::histo::p2d* G4P2ToolsManager::GetP2(G4int id, G4bool warn,
G4bool onlyIfActive) const
{
return GetTInFunction(id, "GetP2", warn, onlyIfActive);
}
@@ -0,0 +1,253 @@
//
// ********************************************************************
// * 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, 02/06/2015 (ivana@ipno.in2p3.fr)
#include "G4HnInformation.hh"
#include "G4PlotManager.hh"
#include "G4AnalysisUtilities.hh"
#include "G4ios.hh"
#if defined(TOOLS_USE_FREETYPE)
#include "toolx/sg/text_freetype"
#include "toolx/xml/xml_style"
#include "tools/xml/wrap_viewplot_fonts_google_style"
//inlib/xml/viewplot.style file embeded in an inline function.
#include "tools/font/lato_regular_ttf"
#include "tools/font/roboto_bold_ttf"
namespace {
// from g4tools/test/viewplot.cpp
//_____________________________________________________________________________
void HD_style(tools::sg::plots& a_plots,float a_line_width) {
std::vector<tools::sg::plotter*> plotters;
a_plots.plotters(plotters);
tools_vforcit(tools::sg::plotter*,plotters,it) {
tools::sg::plotter* _plotter = *it;
_plotter->bins_style(0).line_width = a_line_width;
_plotter->inner_frame_style().line_width = a_line_width;
_plotter->grid_style().line_width = a_line_width;
_plotter->x_axis().line_style().width = a_line_width;
_plotter->x_axis().ticks_style().width = a_line_width;
_plotter->y_axis().line_style().width = a_line_width;
_plotter->y_axis().ticks_style().width = a_line_width;
_plotter->z_axis().line_style().width = a_line_width;
_plotter->z_axis().ticks_style().width = a_line_width;
// needed if font is hershey :
_plotter->title_style().line_width = a_line_width;
_plotter->infos_style().line_width = a_line_width;
_plotter->title_box_style().line_width = a_line_width;
_plotter->x_axis().labels_style().line_width = a_line_width;
_plotter->x_axis().mag_style().line_width = a_line_width;
_plotter->x_axis().title_style().line_width = a_line_width;
_plotter->y_axis().labels_style().line_width = a_line_width;
_plotter->y_axis().mag_style().line_width = a_line_width;
_plotter->y_axis().title_style().line_width = a_line_width;
_plotter->z_axis().labels_style().line_width = a_line_width;
_plotter->z_axis().mag_style().line_width = a_line_width;
_plotter->z_axis().title_style().line_width = a_line_width;
}
}
// from g4tools/test/viewplot.cpp
//_____________________________________________________________________________
void regions_style(tools::sg::plots& a_plots,float a_plotter_scale = 1) {
// Rescale some plotter parameters (for example margins) according to the number of regions.
// We assume that these parameters had been set previously according to one plot per page.
// Then this function must be applied after all the styles had been applied (because
// a plotting style may set these parameters).
float ww_wc = a_plots.width;
float wh_wc = a_plots.height;
float rw_wc = ww_wc/a_plots.cols;
float rh_wc = wh_wc/a_plots.rows;
float cooking = 1.2f; //if increased the data area is diminished.
float wfac = (rw_wc/ww_wc)*cooking;
float hfac = (rh_wc/wh_wc)*cooking;
float label_cooking = 1.6f; //if increased the labels are bigger.
if((a_plots.cols.value()>=4)&&(a_plots.cols.value()>a_plots.rows.value())) label_cooking = 0.9f;
float title_cooking = 1.1f; //extra title cooking.
a_plots.plotter_scale = a_plotter_scale;
std::vector<tools::sg::plotter*> plotters;
a_plots.plotters(plotters);
tools_vforcit(tools::sg::plotter*,plotters,it) {
tools::sg::plotter* _plotter = *it;
_plotter->left_margin = _plotter->left_margin * wfac;
_plotter->right_margin = _plotter->right_margin * wfac;
_plotter->bottom_margin = _plotter->bottom_margin * hfac;
_plotter->top_margin = _plotter->top_margin * hfac;
_plotter->x_axis().tick_length = _plotter->x_axis().tick_length * wfac;
_plotter->y_axis().tick_length = _plotter->y_axis().tick_length * hfac;
_plotter->title_to_axis = _plotter->title_to_axis * hfac;
_plotter->title_height = _plotter->title_height * hfac * title_cooking;
_plotter->x_axis().label_height = _plotter->x_axis().label_height * hfac * label_cooking;
_plotter->y_axis().label_height = _plotter->y_axis().label_height * hfac * label_cooking;
}
}
// from g4tools/test/viewplot.cpp
//_____________________________________________________________________________
bool load_embeded_styles(tools::xml::styles& a_styles) {
std::string ss;
unsigned int linen;
const char** lines = viewplot_fonts_google_style(linen);
for(unsigned int index=0;index<linen;index++) {
std::string s = lines[index];
tools::replace(s,"@@double_quote@@","\"");
tools::replace(s,"@@back_slash@@","\\");
ss += s + "\n";
}
return toolx::xml::load_style_string(a_styles,ss);
}
}
#endif
using namespace G4Analysis;
//
// ctors, dtor
//
//_____________________________________________________________________________
G4PlotManager::G4PlotManager(const G4AnalysisManagerState& state)
: fState(state)
{
#if defined(TOOLS_USE_FREETYPE)
//////////////////////////////////////////////////////////////////////////////
/// plotting, high resolution with freetype fonts and by using styles : //////
//////////////////////////////////////////////////////////////////////////////
fState.Message(kVL1, "... using high resolution with Freetype fonts", "");
//Have vertical A4 :
// unsigned int ww = 2000; //to have better antialising on freetype fonts.
// float A4 = 29.7f/21.0f;
// unsigned int wh = (unsigned int)(float(ww)*A4*0.80);
static toolx::sg::text_freetype ttf;
ttf.add_embedded_font(tools::sg::font_lato_regular_ttf(),tools::font::lato_regular_ttf);
ttf.add_embedded_font(tools::sg::font_roboto_bold_ttf(),tools::font::roboto_bold_ttf);
fViewer = std::make_unique<tools::viewplot>(G4cout, ttf,
fPlotParameters.GetColumns(),
fPlotParameters.GetRows(),
fPlotParameters.GetWidth(),
fPlotParameters.GetHeight());
fViewer->plots().view_border = false;
load_embeded_styles(fViewer->styles());
fViewer->styles().add_colormap("default",tools::sg::style_default_colormap());
fViewer->styles().add_colormap("ROOT",tools::sg::style_ROOT_colormap());
#else
// cretae a viewer with default parameters
fState.Message(kVL1, "... using low resolution with Hershey fonts", "");
fViewer = std::make_unique<tools::viewplot>(G4cout,
fPlotParameters.GetColumns(),
fPlotParameters.GetRows(),
fPlotParameters.GetWidth(),
fPlotParameters.GetHeight());
fViewer->plots().view_border = false;
#endif
}
//
// private methods
//
//_____________________________________________________________________________
G4bool G4PlotManager::WritePage()
{
fState.Message(kVL4, "write a page in", "plot file", fFileName);
#if defined(TOOLS_USE_FREETYPE)
HD_style(fViewer->plots(), 5);
regions_style(fViewer->plots(), fPlotParameters.GetScale());
#endif
G4bool result = fViewer->write_page();
if ( ! result ) {
Warn("Cannot write a page in the plot file " + fFileName,
fkClass, "WritePage");
}
// clear viewers plots
fViewer->plots().init_sg();
//it will recreate the sg::plotters and then reset the styles on new ones.
fState.Message(kVL3, "write a page in", "plot file", fFileName);
return result;
}
//
// public methods
//
//_____________________________________________________________________________
G4bool G4PlotManager::OpenFile(const G4String& fileName)
{
fState.Message(kVL4, "open", "plot file", fileName);
// Keep filename for logging
fFileName = fileName;
G4bool result = fViewer->open_file(fileName);
if ( ! result ) {
Warn("Cannot open plot file " + fileName, fkClass, "OpenFile");
}
fState.Message(kVL1, "open", "plot file", fileName);
return result;
}
//_____________________________________________________________________________
G4bool G4PlotManager::CloseFile()
{
fState.Message(kVL4, "close", "plot file", fFileName);
G4bool result = fViewer->close_file();
if ( ! result ) {
Warn("Cannot close the plot file", fkClass, "CloseFile");
}
fState.Message(kVL1, "close", "plot file", fFileName);
return result;
}
@@ -0,0 +1,169 @@
//
// ********************************************************************
// * 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, 21/10/2015 (ivana@ipno.in2p3.fr)
#include "G4PlotMessenger.hh"
#include "G4PlotParameters.hh"
#include "G4AnalysisUtilities.hh"
#include "G4UIdirectory.hh"
#include "G4UIcommand.hh"
#include "G4UIparameter.hh"
#include "G4UIcmdWithAString.hh"
#include <vector>
using namespace G4Analysis;
using std::to_string;
//_____________________________________________________________________________
G4PlotMessenger::G4PlotMessenger(G4PlotParameters* plotParameters)
: fPlotParameters(plotParameters)
{
fDirectory = std::make_unique<G4UIdirectory>("/analysis/plot/");
fDirectory->SetGuidance("Analysis batch plotting control");
SetStyleCmd();
SetLayoutCmd();
SetDimensionsCmd();
}
//_____________________________________________________________________________
G4PlotMessenger::~G4PlotMessenger() = default;
//
// private functions
//
//_____________________________________________________________________________
void G4PlotMessenger::AddIntParameter(
G4UIcommand& command, G4String name, G4String guidance, G4String range)
{
auto param = new G4UIparameter(name.c_str(), 'i', false);
param->SetGuidance(guidance.c_str());
if (! range.empty()) {
param->SetParameterRange(range);
}
command.SetParameter(param);
}
//_____________________________________________________________________________
void G4PlotMessenger::SetStyleCmd()
{
G4String guidance;
G4String candidates;
#if defined(TOOLS_USE_FREETYPE)
guidance =
"Set plotting style from: \n"
" ROOT_default: ROOT style with high resolution fonts\n"
" hippodraw: hippodraw style with high resolution fonts\n"
" inlib_default: PAW style with low resolution fonts";
candidates =
"ROOT_default hippodraw inlib_default";
#else
guidance =
"Only one plotting style is available in low resolution: \n"
" inlib_default: PAW style with low resolution fonts";
candidates =
"inlib_default";
#endif
fSetStyleCmd = CreateCommand<G4UIcmdWithAString>("setStyle", guidance);
fSetStyleCmd->SetParameterName("Style", false);
fSetStyleCmd->SetCandidates("inlib_default");
}
//_____________________________________________________________________________
void G4PlotMessenger::SetLayoutCmd()
{
fSetLayoutCmd = CreateCommand<G4UIcommand>(
"setLayout",
"Set page layout (number of columns and rows per page).\n"
" Supported layouts:\n"
" columns = 1 .. maxValueAllowed\n"
" rows = 1 .. maxValueAllowed, and >= columns\"");
AddIntParameter(*fSetLayoutCmd, "columns",
"The number of columns in the page layout.",
"columns>=1 && columns<=" + std::to_string(fPlotParameters->GetMaxColumns()));
AddIntParameter(*fSetLayoutCmd, "rows",
"The number of rows in the page layout.",
"rows>=1 && rows<=" + std::to_string(fPlotParameters->GetMaxRows()));
}
//_____________________________________________________________________________
void G4PlotMessenger::SetDimensionsCmd()
{
fSetDimensionsCmd = CreateCommand<G4UIcommand>(
"setDimensions",
"Set the plotter window size (width and height) in pixels.");
AddIntParameter(*fSetDimensionsCmd, "width", "The page width.");
AddIntParameter(*fSetDimensionsCmd, "height", "The page height.");
}
//
// public functions
//
//_____________________________________________________________________________
void G4PlotMessenger::SetNewValue(G4UIcommand* command, G4String newValues)
{
// tokenize parameters in a vector
std::vector<G4String> parameters;
G4Analysis::Tokenize(newValues, parameters);
// check consistency
if ( parameters.size() != command->GetParameterEntries() ) {
// Should never happen but let's check anyway for consistency
G4Analysis::Warn(
"Got wrong number of \"" + command->GetCommandName() +
"\" parameters: " + to_string(parameters.size()) +
" instead of " + to_string(command->GetParameterEntries()) + " expected",
fkClass, "WarnAboutParameters");
return;
}
auto counter = 0;
if ( command == fSetLayoutCmd.get() ) {
auto columns = G4UIcommand::ConvertToInt(parameters[counter++]);
auto rows = G4UIcommand::ConvertToInt(parameters[counter++]);
fPlotParameters->SetLayout(columns, rows);
return;
}
if ( command == fSetDimensionsCmd.get() ) {
auto width = G4UIcommand::ConvertToInt(parameters[counter++]);
auto height = G4UIcommand::ConvertToInt(parameters[counter++]);
fPlotParameters->SetDimensions(width, height);
return;
}
if ( command == fSetStyleCmd.get() ) {
fPlotParameters->SetStyle(newValues);
return;
}
}
@@ -0,0 +1,100 @@
//
// ********************************************************************
// * 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, 21/10/2015 (ivana@ipno.in2p3.fr)
#include "G4PlotParameters.hh"
#include "G4PlotMessenger.hh"
#include "G4AnalysisUtilities.hh"
#include "globals.hh"
using namespace G4Analysis;
using std::to_string;
//_____________________________________________________________________________
G4PlotParameters::G4PlotParameters()
: fMessenger(nullptr),
#if defined(TOOLS_USE_FREETYPE)
fDefaultStyle("ROOT_default"),
fAvailableStyles("ROOT_default hippodrow inlib_default"),
#else
fDefaultStyle("inlib_default"),
fAvailableStyles("inlib_default"),
#endif
fStyle(fDefaultStyle)
{
fMessenger = std::make_unique<G4PlotMessenger>(this);
}
//
// private methods
//
//_____________________________________________________________________________
void G4PlotParameters::SetLayout(G4int columns, G4int rows)
{
if ( columns > rows ||
columns < 1 || columns > fkMaxColumns ||
rows < 1 || rows > fkMaxRows ) {
Warn("Layout: " + to_string(columns) + " x " + to_string(rows) +
" was ignored.\n"
"Supported layouts (columns <= rows): \n" +
" columns = 1 .. " + to_string(fkMaxColumns) + "\n" +
" rows = 1 .. " + to_string(fkMaxRows),
fkClass, "SetLayout");
return;
}
fColumns = columns;
fRows = rows;
}
//_____________________________________________________________________________
void G4PlotParameters::SetDimensions(G4int width, G4int height)
{
fWidth = width;
fHeight = height;
}
//_____________________________________________________________________________
void G4PlotParameters::SetStyle(const G4String& style)
{
// Set style and update scale according to the style selected
if ( fAvailableStyles.find(style) == std::string::npos ) {
Warn("Style: " + style + " was ignored.\n" +
"Supported styles: " + fAvailableStyles,
fkClass, "SetStyle");
return;
}
fStyle = style;
if ( fStyle == "ROOT_default" ) {
fScale = fkDefaultScale;
} else {
fScale = 1.f;
}
}
@@ -0,0 +1,129 @@
//
// ********************************************************************
// * 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, 10/08/2022 (ivana@ipno.in2p3.fr)
#include "G4THnToolsManager.hh"
#include "tools/histo/h1d"
using namespace G4Analysis;
#include <fstream>
// Specialization for H1 type
//_____________________________________________________________________________
template <>
tools::histo::h1d* G4THnToolsManager<1, tools::histo::h1d>::CreateToolsHT(
const G4String& title,
const std::array<G4HnDimension, 1>& bins,
const std::array<G4HnDimensionInformation, 1>& hnInfo)
{
// Apply hn information to bins
auto newBins(bins[kX]);
Update(newBins, hnInfo[kX]);
if (hnInfo[kX].fBinScheme == G4BinScheme::kLinear) {
return new tools::histo::h1d(title, newBins.fNBins, newBins.fMinValue, newBins.fMaxValue);
}
return new tools::histo::h1d(title, newBins.fEdges);
}
//_____________________________________________________________________________
template <>
void G4THnToolsManager<1, tools::histo::h1d>::ConfigureToolsHT(
tools::histo::h1d* ht,
const std::array<G4HnDimension, 1>& bins,
const std::array<G4HnDimensionInformation, 1>& hnInfo)
{
// Apply hn information to bins
auto newBins(bins[kX]);
Update(newBins, hnInfo[kX]);
if (hnInfo[kX].fBinScheme == G4BinScheme::kLinear) {
ht->configure(newBins.fNBins, newBins.fMinValue, newBins.fMaxValue);
return;
}
ht->configure(newBins.fEdges);
}
//_____________________________________________________________________________
template <>
G4bool G4THnToolsManager<1, tools::histo::h1d>::FillHT(
tools::histo::h1d* ht, const G4HnInformation& hnInformation,
std::array<G4double, 1>& value, G4double weight)
{
auto xInfo = hnInformation.GetHnDimensionInformation(kX);
// Apply hn information to value
Update(value[kX], xInfo);
// Fill updated value
ht->fill(value[kX], weight);
return true;
}
//_____________________________________________________________________________
template <>
G4bool G4THnToolsManager<1, tools::histo::h1d>::WriteOnAscii(
std::ofstream& output)
{
// Write selected objects on ASCII file
// According to the implementation by Michel Maire, originally in
// extended examples.
// Do nothing if no histograms are selected
if ( ! GetHnManager()->IsAscii() ) return true;
// Write h1 histograms
auto id = GetHnManager()->GetFirstId();
for (const auto& [h1, info] : *GetTHnVector()) {
if ( ! info->GetAscii() ) {
// skip writing if activation is enabled and H1 is inactivated
id++;
continue;
}
Message(kVL3, "write on ascii", "h1d", info->GetName());
output << "\n 1D histogram " << id++ << ": " << h1->title()
<< "\n \n \t X \t\t Bin Height" << G4endl;
for (G4int j=0; j< G4int(h1->axis().bins()); ++j) {
output << " " << j << "\t"
<< h1->axis().bin_center(j) << "\t"
<< h1->bin_height(j) << G4endl;
}
}
return output.good();
}
@@ -0,0 +1,144 @@
//
// ********************************************************************
// * 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, 10/08/2022 (ivana@ipno.in2p3.fr)
#include "G4THnToolsManager.hh"
#include "tools/histo/h2d"
using namespace G4Analysis;
#include <fstream>
// Specialization for H2 type
//_____________________________________________________________________________
template <>
tools::histo::h2d* G4THnToolsManager<2, tools::histo::h2d>::CreateToolsHT(
const G4String& title,
const std::array<G4HnDimension, 2>& bins,
const std::array<G4HnDimensionInformation, 2>& hnInfo)
{
// Apply hn information to bins
auto newXBins(bins[kX]);
Update(newXBins, hnInfo[kX]);
auto newYBins(bins[kY]);
Update(newYBins, hnInfo[kY]);
if ((hnInfo[kX].fBinScheme == G4BinScheme::kLinear) &&
(hnInfo[kY].fBinScheme == G4BinScheme::kLinear)) {
return new tools::histo::h2d(title,
newXBins.fNBins, newXBins.fMinValue, newXBins.fMaxValue,
newYBins.fNBins, newYBins.fMinValue, newYBins.fMaxValue);
}
return new tools::histo::h2d(title, newXBins.fEdges, newYBins.fEdges);
}
//_____________________________________________________________________________
template <>
void G4THnToolsManager<2, tools::histo::h2d>::ConfigureToolsHT(
tools::histo::h2d* ht,
const std::array<G4HnDimension, 2>& bins,
const std::array<G4HnDimensionInformation, 2>& hnInfo)
{
// Apply hn information to bins
auto newXBins(bins[kX]);
Update(newXBins, hnInfo[kX]);
auto newYBins(bins[kY]);
Update(newYBins, hnInfo[kY]);
if ((hnInfo[kX].fBinScheme == G4BinScheme::kLinear) &&
(hnInfo[kY].fBinScheme == G4BinScheme::kLinear)) {
ht->configure(
newXBins.fNBins, newXBins.fMinValue, newXBins.fMaxValue,
newYBins.fNBins, newYBins.fMinValue, newYBins.fMaxValue);
return;
}
ht->configure(newXBins.fEdges, newYBins.fEdges);
}
//_____________________________________________________________________________
template <>
G4bool G4THnToolsManager<2, tools::histo::h2d>::FillHT(
tools::histo::h2d* ht, const G4HnInformation& hnInformation,
std::array<G4double, 2>& value, G4double weight)
{
auto xInfo = hnInformation.GetHnDimensionInformation(kX);
auto yInfo = hnInformation.GetHnDimensionInformation(kY);
// Apply hn information to value
Update(value[kX], xInfo);
Update(value[kY], yInfo);
// Fill updated value
ht->fill(value[kX], value[kY], weight);
return true;
}
//_____________________________________________________________________________
template <>
G4bool G4THnToolsManager<2, tools::histo::h2d>::WriteOnAscii(
std::ofstream& output)
{
// Write selected objects on ASCII file
// According to the implementation by Michel Maire, originally in
// extended examples.
// Do nothing if no histograms are selected
if ( ! GetHnManager()->IsAscii() ) return true;
// Write h2 histograms
auto id = GetHnManager()->GetFirstId();
for (const auto& [h2, info] : *GetTHnVector()) {
if ( ! info->GetAscii() ) {
// skip writing if activation is enabled and H1 is inactivated
id++;
continue;
}
Message(kVL3, "write on ascii", "h2d", info->GetName());
output << "\n 2D histogram " << id++ << ": " << h2->title()
<< "\n \n \t \t X \t\t Y \t\t Bin Height" << G4endl;
for (G4int j=0; j< G4int(h2->axis_x().bins()); ++j) {
for (G4int k=0; k< G4int(h2->axis_y().bins()); ++k) {
output << " " << j << "\t" << k << "\t"
<< h2->axis_x().bin_center(j) << "\t"
<< h2->axis_y().bin_center(k) << "\t"
<< h2->bin_height(j, k) << G4endl;
}
}
}
return output.good();
}
@@ -0,0 +1,158 @@
//
// ********************************************************************
// * 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, 10/08/2022 (ivana@ipno.in2p3.fr)
#include "G4THnToolsManager.hh"
#include "tools/histo/h3d"
using namespace G4Analysis;
#include <fstream>
// Specialization for H3 type
//_____________________________________________________________________________
template <>
tools::histo::h3d* G4THnToolsManager<3, tools::histo::h3d>::CreateToolsHT(
const G4String& title,
const std::array<G4HnDimension, 3>& bins,
const std::array<G4HnDimensionInformation, 3>& hnInfo)
{
// Apply hn information to bins
auto newXBins(bins[kX]);
Update(newXBins, hnInfo[kX]);
auto newYBins(bins[kY]);
Update(newYBins, hnInfo[kY]);
auto newZBins(bins[kZ]);
Update(newZBins, hnInfo[kZ]);
if ((hnInfo[kX].fBinScheme == G4BinScheme::kLinear) &&
(hnInfo[kY].fBinScheme == G4BinScheme::kLinear) &&
(hnInfo[kZ].fBinScheme == G4BinScheme::kLinear)) {
return new tools::histo::h3d(title,
newXBins.fNBins, newXBins.fMinValue, newXBins.fMaxValue,
newYBins.fNBins, newYBins.fMinValue, newYBins.fMaxValue,
newZBins.fNBins, newZBins.fMinValue, newZBins.fMaxValue);
}
return new tools::histo::h3d(title,
newXBins.fEdges, newYBins.fEdges, newZBins.fEdges);
}
//_____________________________________________________________________________
template <>
void G4THnToolsManager<3, tools::histo::h3d>::ConfigureToolsHT(
tools::histo::h3d* ht,
const std::array<G4HnDimension, 3>& bins,
const std::array<G4HnDimensionInformation, 3>& hnInfo)
{
// Apply hn information to bins
auto newXBins(bins[kX]);
Update(newXBins, hnInfo[kX]);
auto newYBins(bins[kY]);
Update(newYBins, hnInfo[kY]);
auto newZBins(bins[kZ]);
Update(newZBins, hnInfo[kZ]);
if ((hnInfo[kX].fBinScheme == G4BinScheme::kLinear) &&
(hnInfo[kY].fBinScheme == G4BinScheme::kLinear) &&
(hnInfo[kZ].fBinScheme == G4BinScheme::kLinear)) {
ht->configure(
newXBins.fNBins, newXBins.fMinValue, newXBins.fMaxValue,
newYBins.fNBins, newYBins.fMinValue, newYBins.fMaxValue,
newZBins.fNBins, newZBins.fMinValue, newZBins.fMaxValue);
return;
}
ht->configure(newXBins.fEdges, newYBins.fEdges, newZBins.fEdges);
}
//_____________________________________________________________________________
template <>
G4bool G4THnToolsManager<3, tools::histo::h3d>::FillHT(
tools::histo::h3d* ht, const G4HnInformation& hnInformation,
std::array<G4double, 3>& value, G4double weight)
{
auto xInfo = hnInformation.GetHnDimensionInformation(kX);
auto yInfo = hnInformation.GetHnDimensionInformation(kY);
auto zInfo = hnInformation.GetHnDimensionInformation(kZ);
// Apply hn information to value
Update(value[kX], xInfo);
Update(value[kY], yInfo);
Update(value[kZ], zInfo);
// Fill updated value
ht->fill(value[kX], value[kY], value[kZ], weight);
return true;
}
//_____________________________________________________________________________
template <>
G4bool G4THnToolsManager<3, tools::histo::h3d>::WriteOnAscii(
std::ofstream& output)
{
// Write selected objects on ASCII file
// According to the implementation by Michel Maire, originally in
// extended examples.
// Do nothing if no histograms are selected
if ( ! GetHnManager()->IsAscii() ) return true;
// Write h3 histograms
auto id = GetHnManager()->GetFirstId();
for (const auto& [h3, info] : *GetTHnVector()) {
if ( ! info->GetAscii() ) {
// skip writing if activation is enabled and H1 is inactivated
id++;
continue;
}
Message(kVL3, "write on ascii", "h3d", info->GetName());
output << "\n 3D histogram " << id++ << ": " << h3->title()
<< "\n \n \t \t \t X \t\t Y \t\t Z \t\t Bin Height" << G4endl;
for (G4int j=0; j< G4int(h3->axis_x().bins()); ++j) {
for (G4int k=0; k< G4int(h3->axis_y().bins()); ++k) {
for (G4int l=0; l< G4int(h3->axis_y().bins()); ++l) {
output << " " << j << "\t" << k << "\t" << l << "\t"
<< h3->axis_x().bin_center(j) << "\t"
<< h3->axis_y().bin_center(k) << "\t"
<< h3->axis_y().bin_center(l) << "\t"
<< h3->bin_height(j, k, l) << G4endl;
}
}
}
}
return output.good();
}
@@ -0,0 +1,159 @@
//
// ********************************************************************
// * 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, 10/08/2022 (ivana@ipno.in2p3.fr)
#include "G4THnToolsManager.hh"
#include "tools/histo/p1d"
using namespace G4Analysis;
#include <fstream>
// Specialization for P1 type
//_____________________________________________________________________________
template <>
tools::histo::p1d* G4THnToolsManager<2, tools::histo::p1d>::CreateToolsHT(
const G4String& title,
const std::array<G4HnDimension, 2>& bins,
const std::array<G4HnDimensionInformation, 2>& hnInfo)
{
// Apply hn information to bins
auto newXBins(bins[kX]);
Update(newXBins, hnInfo[kX]);
auto newYBins(bins[kY]);
UpdateValues(newYBins, hnInfo[kY]);
if (hnInfo[kX].fBinScheme == G4BinScheme::kLinear) {
if ( newYBins.fMinValue == 0. && newYBins.fMaxValue == 0.) {
return new tools::histo::p1d(
title, newXBins.fNBins, newXBins.fMinValue, newXBins.fMaxValue);
}
return new tools::histo::p1d(
title, newXBins.fNBins, newXBins.fMinValue, newXBins.fMaxValue,
newYBins.fMinValue, newYBins.fMaxValue);
}
if ( newYBins.fMinValue == 0. && newYBins.fMaxValue == 0.) {
return new tools::histo::p1d(title, newXBins.fEdges);
}
return new tools::histo::p1d(title, newXBins.fEdges,
newYBins.fMinValue, newYBins.fMaxValue);
}
//_____________________________________________________________________________
template <>
void G4THnToolsManager<2, tools::histo::p1d>::ConfigureToolsHT(
tools::histo::p1d* ht,
const std::array<G4HnDimension, 2>& bins,
const std::array<G4HnDimensionInformation, 2>& hnInfo)
{
// Apply hn information to bins
auto newXBins(bins[kX]);
Update(newXBins, hnInfo[kX]);
auto newYBins(bins[kY]);
UpdateValues(newYBins, hnInfo[kY]);
if (hnInfo[kX].fBinScheme == G4BinScheme::kLinear) {
if ( newYBins.fMinValue == 0. && newYBins.fMaxValue == 0.) {
ht->configure(
newXBins.fNBins, newXBins.fMinValue, newXBins.fMaxValue);
return;
}
ht->configure(
newXBins.fNBins, newXBins.fMinValue, newXBins.fMaxValue,
newYBins.fMinValue, newYBins.fMaxValue);
return;
}
if ( newYBins.fMinValue == 0. && newYBins.fMaxValue == 0.) {
ht->configure(newXBins.fEdges);
return;
}
ht->configure(newXBins.fEdges, newYBins.fMinValue, newYBins.fMaxValue);
return;
}
//_____________________________________________________________________________
template <>
G4bool G4THnToolsManager<2, tools::histo::p1d>::FillHT(
tools::histo::p1d* ht, const G4HnInformation& hnInformation,
std::array<G4double, 2>& value, G4double weight)
{
auto xInfo = hnInformation.GetHnDimensionInformation(kX);
auto yInfo = hnInformation.GetHnDimensionInformation(kY);
// Apply hn information to value
Update(value[kX], xInfo);
Update(value[kY], yInfo);
// Fill updated value
ht->fill(value[kX], value[kY], weight);
return true;
}
//_____________________________________________________________________________
template <>
G4bool G4THnToolsManager<2, tools::histo::p1d>::WriteOnAscii(
std::ofstream& output)
{
// Write selected objects on ASCII file
// Do nothing if no histograms are selected
if ( ! GetHnManager()->IsAscii() ) return true;
// Write p1 histograms
auto id = GetHnManager()->GetFirstId();
for (const auto& [p1, info] : *GetTHnVector()) {
if ( ! info->GetAscii() ) {
// skip writing if activation is enabled and H1 is inactivated
id++;
continue;
}
Message(kVL3, "write on ascii", "p1d", info->GetName());
output << "\n 1D profile " << id++ << ": " << p1->title()
<< "\n \n \t \t X \t\t MeanY" << G4endl;
for (G4int j=0; j< G4int(p1->axis().bins()); ++j) {
auto sw = p1->bin_Sw(j);
auto svw = p1->bin_Svw(j);
auto mean = ( sw != 0. ) ? (svw / sw) : 0.;
output << " " << j << "\t"
<< p1->axis().bin_center(j) << "\t"
<< mean << G4endl;
}
}
return output.good();
}
@@ -0,0 +1,174 @@
//
// ********************************************************************
// * 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, 10/08/2022 (ivana@ipno.in2p3.fr)
#include "G4THnToolsManager.hh"
#include "tools/histo/p2d"
using namespace G4Analysis;
#include <fstream>
// Specialization for P2 type
//_____________________________________________________________________________
template <>
tools::histo::p2d* G4THnToolsManager<3, tools::histo::p2d>::CreateToolsHT(
const G4String& title,
const std::array<G4HnDimension, 3>& bins,
const std::array<G4HnDimensionInformation, 3>& hnInfo)
{
// Apply hn information to bins
auto newXBins(bins[kX]);
Update(newXBins, hnInfo[kX]);
auto newYBins(bins[kY]);
Update(newYBins, hnInfo[kY]);
auto newZBins(bins[kZ]);
UpdateValues(newZBins, hnInfo[kZ]);
if ((hnInfo[kX].fBinScheme == G4BinScheme::kLinear) &&
(hnInfo[kY].fBinScheme == G4BinScheme::kLinear)) {
if ( newZBins.fMinValue == 0. && newZBins.fMaxValue == 0.) {
return new tools::histo::p2d(
title, newXBins.fNBins, newXBins.fMinValue, newXBins.fMaxValue,
newYBins.fNBins, newYBins.fMinValue, newYBins.fMaxValue);
}
return new tools::histo::p2d(
title, newXBins.fNBins, newXBins.fMinValue, newXBins.fMaxValue,
newYBins.fNBins, newYBins.fMinValue, newYBins.fMaxValue,
newZBins.fMinValue, newZBins.fMaxValue);
}
if ( newZBins.fMinValue == 0. && newZBins.fMaxValue == 0.) {
return new tools::histo::p2d(title, newXBins.fEdges, newYBins.fEdges);
}
return new tools::histo::p2d(title, newXBins.fEdges, newYBins.fEdges,
newZBins.fMinValue, newZBins.fMaxValue);
}
//_____________________________________________________________________________
template <>
void G4THnToolsManager<3, tools::histo::p2d>::ConfigureToolsHT(
tools::histo::p2d* ht,
const std::array<G4HnDimension, 3>& bins,
const std::array<G4HnDimensionInformation, 3>& hnInfo)
{
// Apply hn information to bins
auto newXBins(bins[kX]);
Update(newXBins, hnInfo[kX]);
auto newYBins(bins[kY]);
Update(newYBins, hnInfo[kY]);
auto newZBins(bins[kZ]);
UpdateValues(newZBins, hnInfo[kZ]);
if ((hnInfo[kX].fBinScheme == G4BinScheme::kLinear) &&
(hnInfo[kY].fBinScheme == G4BinScheme::kLinear)) {
if ( newZBins.fMinValue == 0. && newZBins.fMaxValue == 0.) {
ht->configure(
newXBins.fNBins, newXBins.fMinValue, newXBins.fMaxValue,
newYBins.fNBins, newYBins.fMinValue, newYBins.fMaxValue);
return;
}
ht->configure(
newXBins.fNBins, newXBins.fMinValue, newXBins.fMaxValue,
newYBins.fNBins, newYBins.fMinValue, newYBins.fMaxValue,
newZBins.fMinValue, newZBins.fMaxValue);
return;
}
if ( newZBins.fMinValue == 0. && newZBins.fMaxValue == 0.) {
ht->configure(newXBins.fEdges, newYBins.fEdges);
return;
}
ht->configure(newXBins.fEdges, newYBins.fEdges,
newZBins.fMinValue, newZBins.fMaxValue);
}
//_____________________________________________________________________________
template <>
G4bool G4THnToolsManager<3, tools::histo::p2d>::FillHT(
tools::histo::p2d* ht, const G4HnInformation& hnInformation,
std::array<G4double, 3>& value, G4double weight)
{
auto xInfo = hnInformation.GetHnDimensionInformation(kX);
auto yInfo = hnInformation.GetHnDimensionInformation(kY);
auto zInfo = hnInformation.GetHnDimensionInformation(kZ);
// Apply hn information to value
Update(value[kX], xInfo);
Update(value[kY], yInfo);
Update(value[kZ], zInfo);
// Fill updated value
ht->fill(value[kX], value[kY], value[kZ], weight);
return true;
}
//_____________________________________________________________________________
template <>
G4bool G4THnToolsManager<3, tools::histo::p2d>::WriteOnAscii(
std::ofstream& output)
{
// Write selected objects on ASCII file
// Do nothing if no histograms are selected
if ( ! GetHnManager()->IsAscii() ) return true;
// Write p2 histograms
auto id = GetHnManager()->GetFirstId();
for (const auto& [p2, info] : *GetTHnVector()) {
if ( ! info->GetAscii() ) {
// skip writing if activation is enabled and H1 is inactivated
id++;
continue;
}
Message(kVL3, "write on ascii", "p2d", info->GetName());
output << "\n 2D profile " << id++ << ": " << p2->title()
<< "\n \n \t \t X \t\t Y \t\t MeanZ" << G4endl;
for (G4int j=0; j< G4int(p2->axis_x().bins()); ++j) {
for (G4int k=0; k< G4int(p2->axis_y().bins()); ++k) {
auto sw = p2->bin_Sw(j, k);
auto svw = p2->bin_Svw(j, k);
auto mean = ( sw != 0. ) ? (svw / sw) : 0.;
output << " " << j << "\t" << k << "\t"
<< p2->axis_x().bin_center(j) << "\t"
<< p2->axis_y().bin_center(k) << "\t"
<< mean << G4endl;
}
}
}
return output.good();
}
@@ -27,11 +27,7 @@
// Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
#include "G4ToolsAnalysisManager.hh"
#include "G4H1ToolsManager.hh"
#include "G4H2ToolsManager.hh"
#include "G4H3ToolsManager.hh"
#include "G4P1ToolsManager.hh"
#include "G4P2ToolsManager.hh"
#include "G4VNtupleFileManager.hh"
#include "G4PlotManager.hh"
#include "G4MPIToolsManager.hh"
#include "G4AnalysisUtilities.hh"
@@ -66,11 +62,11 @@ G4ToolsAnalysisManager::G4ToolsAnalysisManager(const G4String& type)
fgToolsInstance = this;
// Create managers
fH1Manager = new G4H1ToolsManager(fState);
fH2Manager = new G4H2ToolsManager(fState);
fH3Manager = new G4H3ToolsManager(fState);
fP1Manager = new G4P1ToolsManager(fState);
fP2Manager = new G4P2ToolsManager(fState);
fH1Manager = new G4THnToolsManager<kDim1, tools::histo::h1d>(fState);
fH2Manager = new G4THnToolsManager<kDim2, tools::histo::h2d>(fState);
fH3Manager = new G4THnToolsManager<kDim3, tools::histo::h3d>(fState);
fP1Manager = new G4THnToolsManager<kDim2, tools::histo::p1d>(fState);
fP2Manager = new G4THnToolsManager<kDim3, tools::histo::p2d>(fState);
// The managers will be deleted by the base class
// Set managers to base class which takes then their ownership
@@ -81,10 +77,7 @@ G4ToolsAnalysisManager::G4ToolsAnalysisManager(const G4String& type)
SetP2Manager(fP2Manager);
// Plot manager
SetPlotManager(std::make_shared<G4PlotManager>(fState));
// Messenger
fMessenger = std::make_unique<G4ToolsAnalysisMessenger>(this);
fPlotManager = std::make_unique<G4PlotManager>(fState);
}
//_____________________________________________________________________________
@@ -95,91 +88,11 @@ G4ToolsAnalysisManager::~G4ToolsAnalysisManager()
}
//
// protected methods
// private methods
//
//_____________________________________________________________________________
G4bool G4ToolsAnalysisManager::PlotImpl()
{
// Only master thread performs plotting
if ( G4Threading::IsWorkerThread() ) return true;
auto result = true;
// Open output file
fPlotManager->OpenFile(fVFileManager->GetPlotFileName());
// H1
result
&= fPlotManager->PlotAndWrite<tools::histo::h1d>(fH1Manager->GetH1Vector(),
fH1Manager->GetHnVector());
// H2
result
&= fPlotManager->PlotAndWrite<tools::histo::h2d>(fH2Manager->GetH2Vector(),
fH2Manager->GetHnVector());
// H3
// not yet available in tools
// P1
result
&= fPlotManager->PlotAndWrite<tools::histo::p1d>(fP1Manager->GetP1Vector(),
fP1Manager->GetHnVector());
// P2
// not yet available in tools
// Close output file
result &= fPlotManager->CloseFile();
return result;
}
//_____________________________________________________________________________
G4bool G4ToolsAnalysisManager::MergeImpl(tools::histo::hmpi* hmpi)
{
// if ( G4Threading::IsWorkerThread() ) return true;
if ( ! hmpi ) return false;
auto result = true;
// Create MPI manager
G4MPIToolsManager mpiToolsManager(fState, hmpi);
// H1
result
&= mpiToolsManager.Merge<tools::histo::h1d>(fH1Manager->GetH1Vector(),
fH1Manager->GetHnVector());
// H2
result
&= mpiToolsManager.Merge<tools::histo::h2d>(fH2Manager->GetH2Vector(),
fH2Manager->GetHnVector());
// H3
result
&= mpiToolsManager.Merge<tools::histo::h3d>(fH3Manager->GetH3Vector(),
fH3Manager->GetHnVector());
// P1
result
&= mpiToolsManager.Merge<tools::histo::p1d>(fP1Manager->GetP1Vector(),
fP1Manager->GetHnVector());
// P2
result
&= mpiToolsManager.Merge<tools::histo::p2d>(fP2Manager->GetP2Vector(),
fP2Manager->GetHnVector());
return result;
}
//_____________________________________________________________________________
G4bool G4ToolsAnalysisManager::WriteImpl()
G4bool G4ToolsAnalysisManager::WriteHns()
{
// Nothing to be done on worker
if ( G4Threading::IsWorkerThread() ) return false;
@@ -187,17 +100,17 @@ G4bool G4ToolsAnalysisManager::WriteImpl()
auto result = true;
// Write all histograms/profile on master
result &=WriteT(fH1Manager->GetH1Vector(), fH1Manager->GetHnVector());
result &=WriteT(fH2Manager->GetH2Vector(), fH2Manager->GetHnVector());
result &=WriteT(fH3Manager->GetH3Vector(), fH3Manager->GetHnVector());
result &=WriteT(fP1Manager->GetP1Vector(), fP1Manager->GetHnVector());
result &=WriteT(fP2Manager->GetP2Vector(), fP2Manager->GetHnVector());
result &= WriteT(fH1Manager->GetTHnVectorRef());
result &= WriteT(fH2Manager->GetTHnVectorRef());
result &= WriteT(fH3Manager->GetTHnVectorRef());
result &= WriteT(fP1Manager->GetTHnVectorRef());
result &= WriteT(fP2Manager->GetTHnVectorRef());
return result;
}
//_____________________________________________________________________________
G4bool G4ToolsAnalysisManager::ResetImpl()
G4bool G4ToolsAnalysisManager::ResetHns()
{
// Reset histograms and profiles
@@ -213,24 +126,12 @@ G4bool G4ToolsAnalysisManager::ResetImpl()
}
//_____________________________________________________________________________
void G4ToolsAnalysisManager::ClearImpl()
{
// Reset histograms and profiles
fH1Manager->ClearData();
fH2Manager->ClearData();
fH3Manager->ClearData();
fP1Manager->ClearData();
fP2Manager->ClearData();
}
//_____________________________________________________________________________
G4bool G4ToolsAnalysisManager::Merge()
G4bool G4ToolsAnalysisManager::MergeHns()
{
// Nothing to be done on master
if ( ! G4Threading::IsWorkerThread() ) return false;
if ( ! fgMasterToolsInstance ) {
if (fgMasterToolsInstance == nullptr) {
if (! IsEmpty() ) {
Warn("No master G4AnalysisManager instance exists.\n"
"Histogram/profile data will not be merged.",
@@ -254,6 +155,204 @@ G4bool G4ToolsAnalysisManager::Merge()
return true;
}
//
// protected methods
//
//_____________________________________________________________________________
G4bool G4ToolsAnalysisManager::OpenFileImpl(const G4String& fileName)
{
// Create ntuple manager(s)
// and set it to base class which takes then their ownership
SetNtupleManager(fVNtupleFileManager->CreateNtupleManager());
auto result = true;
// Open file
if ( fVNtupleFileManager->GetMergeMode() != G4NtupleMergeMode::kSlave ) {
result &= fVFileManager->OpenFile(fileName);
}
// Open ntuple files and create ntuples from bookings
result &= fVNtupleFileManager->ActionAtOpenFile(fVFileManager->GetFullFileName());
return result;
}
//_____________________________________________________________________________
G4bool G4ToolsAnalysisManager::WriteImpl()
{
Message(kVL4, "write", "files");
auto result = true;
if ( G4Threading::IsWorkerThread() ) {
result &= MergeHns();
}
else {
// Open all files registered with objects
fVFileManager->OpenFiles();
// Write all histograms/profile on master
result &= WriteHns();
}
// Ntuples
if (fVNtupleFileManager) {
result &= fVNtupleFileManager->ActionAtWrite();
}
// Files
if ( (fVNtupleFileManager == nullptr) ||
(fVNtupleFileManager->GetMergeMode() != G4NtupleMergeMode::kSlave) ) {
result &= fVFileManager->WriteFiles();
}
// Write ASCII if activated
if ( IsAscii() ) {
result &= WriteAscii(fVFileManager->GetFileName());
}
Message(kVL3, "write", "files", "", result);
return result;
}
//_____________________________________________________________________________
G4bool G4ToolsAnalysisManager::CloseFileImpl(G4bool reset)
{
Message(kVL4, "close", "files");
auto result = true;
if (fVNtupleFileManager) {
result &= fVNtupleFileManager->ActionAtCloseFile();
}
// close file
if ( (fVNtupleFileManager == nullptr) ||
(fVNtupleFileManager->GetMergeMode() != G4NtupleMergeMode::kSlave) ) {
if ( ! fVFileManager->CloseFiles() ) {
Warn("Closing files failed", fkClass, "CloseFileImpl");
result = false;
}
}
// delete empty files
if ( ! fVFileManager->DeleteEmptyFiles() ) {
Warn("Deleting empty files failed", fkClass, "CloseFileImpl");
result = false;
}
// reset histograms
if ( reset ) {
if ( ! Reset() ) {
Warn("Resetting data failed", fkClass, "CloseFileImpl");
result = false;
}
}
Message(kVL3, "close", "files", "", result);
return result;
}
//_____________________________________________________________________________
G4bool G4ToolsAnalysisManager::ResetImpl()
{
// Reset histograms and ntuple
Message(kVL4, "reset", "");
auto result = true;
result &= ResetHns();
if ( fVNtupleFileManager != nullptr ) {
result &= fVNtupleFileManager->Reset();
}
Message(kVL3, "reset", "", "", result);
return result;
}
//_____________________________________________________________________________
void G4ToolsAnalysisManager::ClearImpl()
{
// Reset histograms and profiles
fH1Manager->ClearData();
fH2Manager->ClearData();
fH3Manager->ClearData();
fP1Manager->ClearData();
fP2Manager->ClearData();
}
//_____________________________________________________________________________
G4bool G4ToolsAnalysisManager::PlotImpl()
{
// Only master thread performs plotting
if ( G4Threading::IsWorkerThread() ) return true;
auto result = true;
// Open output file
fPlotManager->OpenFile(fVFileManager->GetPlotFileName());
// H1
result
&= fPlotManager->PlotAndWrite<tools::histo::h1d>(fH1Manager->GetTHnVectorRef());
// H2
result
&= fPlotManager->PlotAndWrite<tools::histo::h2d>(fH2Manager->GetTHnVectorRef());
// H3
// not yet available in tools
// P1
result
&= fPlotManager->PlotAndWrite<tools::histo::p1d>(fP1Manager->GetTHnVectorRef());
// P2
// not yet available in tools
// Close output file
result &= fPlotManager->CloseFile();
return result;
}
//_____________________________________________________________________________
G4bool G4ToolsAnalysisManager::MergeImpl(tools::histo::hmpi* hmpi)
{
// if ( G4Threading::IsWorkerThread() ) return true;
if (hmpi == nullptr) return false;
auto result = true;
// Create MPI manager
G4MPIToolsManager mpiToolsManager(fState, hmpi);
// H1
result &= mpiToolsManager.Merge<tools::histo::h1d>(fH1Manager->GetTHnVectorRef());
// H2
result &= mpiToolsManager.Merge<tools::histo::h2d>(fH2Manager->GetTHnVectorRef());
// H3
result &= mpiToolsManager.Merge<tools::histo::h3d>(fH3Manager->GetTHnVectorRef());
// P1
result &= mpiToolsManager.Merge<tools::histo::p1d>(fP1Manager->GetTHnVectorRef());
// P2
result &= mpiToolsManager.Merge<tools::histo::p2d>(fP2Manager->GetTHnVectorRef());
return result;
}
//_____________________________________________________________________________
G4bool G4ToolsAnalysisManager::IsEmpty()
{
@@ -1,125 +0,0 @@
//
// ********************************************************************
// * 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, 29/10/2021 (ivana@ipno.in2p3.fr)
#include "G4ToolsAnalysisMessenger.hh"
#include "G4ToolsAnalysisManager.hh"
#include "G4AnalysisUtilities.hh"
#include "G4AnalysisMessengerHelper.hh"
#include "G4UIcommand.hh"
#include "G4UIparameter.hh"
using namespace G4Analysis;
//_____________________________________________________________________________
G4ToolsAnalysisMessenger::G4ToolsAnalysisMessenger(G4ToolsAnalysisManager* manager)
: G4UImessenger(),
fManager(manager)
{
// Create get commands
G4AnalysisMessengerHelper helper("h1");
fGetH1Cmd = helper.CreateGetCommand(this);
fGetH1VectorCmd = helper.CreateGetVectorCommand(this);
helper.SetHnType("h2");
fGetH2Cmd = helper.CreateGetCommand(this);
fGetH2VectorCmd = helper.CreateGetVectorCommand(this);
helper.SetHnType("h3");
fGetH3Cmd = helper.CreateGetCommand(this);
fGetH3VectorCmd = helper.CreateGetVectorCommand(this);
helper.SetHnType("p1");
fGetP1Cmd = helper.CreateGetCommand(this);
fGetP1VectorCmd = helper.CreateGetVectorCommand(this);
helper.SetHnType("p2");
fGetP2Cmd = helper.CreateGetCommand(this);
fGetP2VectorCmd = helper.CreateGetVectorCommand(this);
}
//_____________________________________________________________________________
G4ToolsAnalysisMessenger::~G4ToolsAnalysisMessenger() = default;
///
// public functions
//
//_____________________________________________________________________________
G4String G4ToolsAnalysisMessenger::GetCurrentValue (G4UIcommand* command)
{
if ( command == fGetH1Cmd.get() ) return fH1Value;
if ( command == fGetH2Cmd.get() ) return fH2Value;
if ( command == fGetH3Cmd.get() ) return fH3Value;
if ( command == fGetP1Cmd.get() ) return fP1Value;
if ( command == fGetP2Cmd.get() ) return fP2Value;
if ( command == fGetH1VectorCmd.get() ) return fH1VectorValue;
if ( command == fGetH2VectorCmd.get() ) return fH2VectorValue;
if ( command == fGetH3VectorCmd.get() ) return fH3VectorValue;
if ( command == fGetP1VectorCmd.get() ) return fP1VectorValue;
if ( command == fGetP2VectorCmd.get() ) return fP2VectorValue;
return "";
}
//_____________________________________________________________________________
void G4ToolsAnalysisMessenger::SetNewValue(G4UIcommand* command, G4String value)
{
auto id = G4UIcommand::ConvertToInt(value);
if ( command == fGetH1Cmd.get() ) {
fH1Value = GetHnAddress(id, fManager->fH1Manager);
}
else if ( command == fGetH2Cmd.get() ) {
fH2Value = GetHnAddress(id, fManager->fH2Manager);
}
else if ( command == fGetH3Cmd.get() ) {
fH3Value = GetHnAddress(id, fManager->fH3Manager);
}
else if ( command == fGetP1Cmd.get() ) {
fP1Value = GetHnAddress(id, fManager->fP1Manager);
}
else if ( command == fGetP2Cmd.get() ) {
fP2Value = GetHnAddress(id, fManager->fP2Manager);
}
if ( command == fGetH1VectorCmd.get() ) {
fH1VectorValue = GetHnVectorAddress(fManager->fH1Manager);
}
else if ( command == fGetH2VectorCmd.get() ) {
fH2VectorValue = GetHnVectorAddress(fManager->fH2Manager);
}
else if ( command == fGetH3VectorCmd.get() ) {
fH3VectorValue = GetHnVectorAddress(fManager->fH3Manager);
}
else if ( command == fGetP1VectorCmd.get() ) {
fP1VectorValue = GetHnVectorAddress(fManager->fP1Manager);
}
else if ( command == fGetP2VectorCmd.get() ) {
fP2VectorValue = GetHnVectorAddress(fManager->fP2Manager);
}
}
@@ -27,22 +27,17 @@
// Author: Ivana Hrivnacova, 20/07/2015 (ivana@ipno.in2p3.fr)
#include "G4ToolsAnalysisReader.hh"
#include "G4H1ToolsManager.hh"
#include "G4H2ToolsManager.hh"
#include "G4H3ToolsManager.hh"
#include "G4P1ToolsManager.hh"
#include "G4P2ToolsManager.hh"
//_____________________________________________________________________________
G4ToolsAnalysisReader::G4ToolsAnalysisReader(const G4String& type)
: G4VAnalysisReader(type)
{
// Create managers
fH1Manager = new G4H1ToolsManager(fState);
fH2Manager = new G4H2ToolsManager(fState);
fH3Manager = new G4H3ToolsManager(fState);
fP1Manager = new G4P1ToolsManager(fState);
fP2Manager = new G4P2ToolsManager(fState);
fH1Manager = new G4THnToolsManager<1, tools::histo::h1d>(fState);
fH2Manager = new G4THnToolsManager<2, tools::histo::h2d>(fState);
fH3Manager = new G4THnToolsManager<3, tools::histo::h3d>(fState);
fP1Manager = new G4THnToolsManager<2, tools::histo::p1d>(fState);
fP2Manager = new G4THnToolsManager<3, tools::histo::p2d>(fState);
// The managers will be deleted by the base class
// Set managers to base class which takes then their ownership