// // ******************************************************************** // * 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 "G4AnalysisUtilities.hh" using G4Analysis::kX; using G4Analysis::kY; using G4Analysis::kZ; using G4Analysis::kMaxDim; using G4Analysis::CheckDimensions; using G4Analysis::GetHnType; using G4Analysis::IsProfile; using G4Analysis::Warn; //_____________________________________________________________________________ template const std::array G4THnToolsManager::fkKeyAxisTitle = { "axis_x.title", "axis_y.title", "axis_z.title" }; //_____________________________________________________________________________ template G4THnToolsManager::G4THnToolsManager(const G4AnalysisManagerState& state) : G4THnManager(state) { fMessenger = std::make_unique>(this); GetHnManager()->CreateMessenger(); } //_____________________________________________________________________________ template void G4THnToolsManager::UpdateInformation(G4HnInformation* hnInformation, const std::array& hnInfo) { for (unsigned int idim = 0; idim < DIM; ++idim) { hnInformation->SetDimension(idim, hnInfo[idim]); } } //_____________________________________________________________________________ template G4HnInformation* G4THnToolsManager::CreateInformation(const G4String& name, const std::array& hnInfo) { auto hnInformation = new G4HnInformation(name, DIM); for (unsigned int idim = 0; idim < DIM; ++idim) { hnInformation->AddDimension(hnInfo[idim]); } return hnInformation; } //_____________________________________________________________________________ template void G4THnToolsManager::AddAnnotation(HT* ht, const std::array& hnInfo) { for (unsigned int idim = 0; idim < DIM; ++idim) { G4String axisTitle; G4Analysis::UpdateTitle(axisTitle, hnInfo[idim]); ht->add_annotation(fkKeyAxisTitle[idim], axisTitle); } } //_____________________________________________________________________________ template G4bool G4THnToolsManager::CheckName(const G4String& name) const { if (name.size() == 0u) { G4Analysis::Warn("Empty " + GetHnType() + " name is not allowed.\n" + GetHnType() + " was not created.", fkClass, "CheckName"); return false; } return true; } //_____________________________________________________________________________ template G4int G4THnToolsManager::Create( const G4String& name, const G4String& title, const std::array& bins, const std::array& hnInfo) { // Check parameters if ((! CheckName(name)) || (! CheckDimensions(bins, hnInfo, IsProfile()))) { return G4Analysis::kInvalidId; } Message(G4Analysis::kVL4, "create", GetHnType(), name); auto ht = CreateToolsHT(title, bins, hnInfo); // Add annotation AddAnnotation(ht, hnInfo); // Create Hn information auto info = CreateInformation(name, hnInfo); // Register histogram & info auto id = RegisterT(name, ht, info); Message(G4Analysis::kVL2, "create", GetHnType(), name); return id; } //_____________________________________________________________________________ template G4bool G4THnToolsManager::Set(G4int id, const std::array& bins, const std::array& hnInfo) { // Check parameters if (! CheckDimensions(bins, hnInfo, IsProfile())) { return false; } auto [ht, info] = GetTHnInFunction(id, "Set" + GetHnType(), false, false); if (ht == nullptr) return false; Message(G4Analysis::kVL4, "configure", GetHnType(), info->GetName()); // Configure tools h2 ConfigureToolsHT(ht, bins, hnInfo); // Add annotation AddAnnotation(ht, hnInfo); // Update information UpdateInformation(info, hnInfo); // Set activation GetHnManager()->SetActivation(id, true); return true; } //_____________________________________________________________________________ template G4bool G4THnToolsManager::Scale(G4int id, G4double factor) { auto ht = GetTInFunction(id, "Scale" + GetHnType(), false, false); if (ht == nullptr) return false; return ht->scale(factor); } //_____________________________________________________________________________ template G4bool G4THnToolsManager::Fill(G4int id, std::array value, G4double weight) { auto [ht, info] = GetTHnInFunction(id, "Fill"+ GetHnType(), true, false); if (ht == nullptr) { Warn("Failed to fill " + GetHnType() + " id " + std::to_string(id) + ". Histogram does not exist.", fkClass, "Fill"); return false; } if ( G4THnManager::fState.GetIsActivation() && ( ! info->GetActivation() ) ) { return false; } std::array newValue(value); auto result = FillHT(ht, *info, newValue, weight); if ( IsVerbose(G4Analysis::kVL4) ) { // compose message G4String dims("xyz"); G4String message = " id " + to_string(id); for (unsigned int idim = 0; idim < DIM; ++idim) { auto xyz = dims.substr(idim,1); message += " " + xyz + " " + to_string(value[idim]) + " " + xyz + "fcn(" + xyz + "value/" + xyz + "unit) " + to_string(newValue[idim]); } message += " weight " + to_string(weight); Message(G4Analysis::kVL4, "fill", GetHnType(), message); } return result; } //_____________________________________________________________________________ template G4int G4THnToolsManager::GetId(const G4String& name, G4bool warn) const { return GetTId(name, warn); } //_____________________________________________________________________________ template G4int G4THnToolsManager::GetNofHns(G4bool onlyIfExist) const { return G4THnManager::GetNofHns(onlyIfExist); } //_____________________________________________________________________________ template G4int G4THnToolsManager::GetNbins(unsigned int idim, G4int id) const { auto ht = GetTInFunction(id, "GetNbins"); if (ht == nullptr) return 0; return ht->get_axis(idim).bins(); } //_____________________________________________________________________________ template G4double G4THnToolsManager::GetMinValue(unsigned int idim, G4int id) const { auto ht = GetTInFunction(id, "GetMinValue"); if (ht == nullptr) return 0.; return ht->get_axis(idim).lower_edge(); } //_____________________________________________________________________________ template G4double G4THnToolsManager::GetMaxValue(unsigned int idim, G4int id) const { auto ht = GetTInFunction(id, "GetMaxValue"); if (ht == nullptr) return 0.; return ht->get_axis(idim).upper_edge(); } //_____________________________________________________________________________ template G4double G4THnToolsManager::GetWidth(unsigned int idim, G4int id) const { auto ht = GetTInFunction(id, "GetWidth", true, false); if (ht == nullptr) return 0.; auto nbins = ht->get_axis(idim).bins(); if (nbins == 0u) { Warn("nbins = 0 ! for " + GetHnType(), fkClass, "GetWidth"); return 0.; } return ( ht->get_axis(idim).upper_edge() - ht->get_axis(idim).lower_edge() )/nbins; } //_____________________________________________________________________________ template G4bool G4THnToolsManager::SetTitle(G4int id, const G4String& title) { auto ht = GetTInFunction(id, "SetTitle"); if (ht == nullptr) return false; return ht->set_title(title); } //_____________________________________________________________________________ template G4bool G4THnToolsManager::SetAxisTitle(unsigned int idim, G4int id, const G4String& title) { auto ht = GetTInFunction(id, "SetAxisTitle"); if (ht == nullptr) return false; ht->add_annotation(fkKeyAxisTitle[idim], title); return true; } //_____________________________________________________________________________ template G4String G4THnToolsManager::GetTitle(G4int id) const { auto ht = GetTInFunction(id, "GetTitle"); if (ht == nullptr) return G4String(); return ht->title(); } //_____________________________________________________________________________ template G4String G4THnToolsManager::GetAxisTitle(unsigned int idim, G4int id) const { auto ht = GetTInFunction(id, "GetAxisTitle"); if (ht == nullptr) return G4String(); G4String title; G4bool result = ht->annotation(fkKeyAxisTitle[idim], title); if ( ! result ) { Warn("Got wrong dimension " + to_string(idim) + " for " + GetHnType(), fkClass, "GetAxisTitle"); return {}; } return title; } //_____________________________________________________________________________ template G4bool G4THnToolsManager::List(std::ostream& output, G4bool onlyIfActive) { return G4THnManager::List(output, onlyIfActive); } //_____________________________________________________________________________ template G4bool G4THnToolsManager::Delete(G4int id, G4bool keepSetting) { G4String message = " id " + to_string(id); Message(G4Analysis::kVL4, "delete", GetHnType(), message); auto result = G4THnManager::DeleteT(id, keepSetting); Message(G4Analysis::kVL2, "delete", GetHnType(), message, result); return result; } //_____________________________________________________________________________ template std::shared_ptr G4THnToolsManager::GetHnManager() { return G4THnManager::fHnManager; } //_____________________________________________________________________________ template const std::shared_ptr G4THnToolsManager::GetHnManager() const { return G4THnManager::fHnManager; }