File indexing completed on 2025-01-18 09:59:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #include "G4AnalysisUtilities.hh"
0030
0031 using G4Analysis::kX;
0032 using G4Analysis::kY;
0033 using G4Analysis::kZ;
0034 using G4Analysis::kMaxDim;
0035 using G4Analysis::CheckDimensions;
0036 using G4Analysis::GetHnType;
0037 using G4Analysis::IsProfile;
0038 using G4Analysis::Warn;
0039
0040
0041 template <unsigned int DIM, typename HT>
0042 const std::array<std::string, kMaxDim> G4THnToolsManager<DIM, HT>::fkKeyAxisTitle =
0043 { "axis_x.title", "axis_y.title", "axis_z.title" };
0044
0045
0046 template <unsigned int DIM, typename HT>
0047 G4THnToolsManager<DIM, HT>::G4THnToolsManager(const G4AnalysisManagerState& state)
0048 : G4THnManager<HT>(state)
0049 {
0050 fMessenger = std::make_unique<G4THnMessenger<DIM, HT>>(this);
0051 GetHnManager()->CreateMessenger();
0052 }
0053
0054
0055 template <unsigned int DIM, typename HT>
0056 void G4THnToolsManager<DIM, HT>::UpdateInformation(G4HnInformation* hnInformation,
0057 const std::array<G4HnDimensionInformation, DIM>& hnInfo)
0058 {
0059 for (unsigned int idim = 0; idim < DIM; ++idim) {
0060 hnInformation->SetDimension(idim, hnInfo[idim]);
0061 }
0062 }
0063
0064
0065 template <unsigned int DIM, typename HT>
0066 G4HnInformation* G4THnToolsManager<DIM, HT>::CreateInformation(const G4String& name,
0067 const std::array<G4HnDimensionInformation, DIM>& hnInfo)
0068 {
0069 auto hnInformation = new G4HnInformation(name, DIM);
0070 for (unsigned int idim = 0; idim < DIM; ++idim) {
0071 hnInformation->AddDimension(hnInfo[idim]);
0072 }
0073
0074 return hnInformation;
0075 }
0076
0077
0078 template <unsigned int DIM, typename HT>
0079 void G4THnToolsManager<DIM, HT>::AddAnnotation(HT* ht,
0080 const std::array<G4HnDimensionInformation, DIM>& hnInfo)
0081 {
0082 for (unsigned int idim = 0; idim < DIM; ++idim) {
0083 G4String axisTitle;
0084 G4Analysis::UpdateTitle(axisTitle, hnInfo[idim]);
0085
0086 ht->add_annotation(fkKeyAxisTitle[idim], axisTitle);
0087 }
0088 }
0089
0090
0091 template <unsigned int DIM, typename HT>
0092 G4bool G4THnToolsManager<DIM, HT>::CheckName(const G4String& name) const
0093 {
0094 if (name.size() == 0u) {
0095 G4Analysis::Warn("Empty " + GetHnType<HT>() + " name is not allowed.\n" +
0096 GetHnType<HT>() + " was not created.", fkClass, "CheckName");
0097 return false;
0098 }
0099 return true;
0100 }
0101
0102
0103 template <unsigned int DIM, typename HT>
0104 G4int G4THnToolsManager<DIM, HT>::Create(
0105 const G4String& name, const G4String& title,
0106 const std::array<G4HnDimension, DIM>& bins,
0107 const std::array<G4HnDimensionInformation, DIM>& hnInfo)
0108 {
0109
0110 if ((! CheckName(name)) ||
0111 (! CheckDimensions<DIM>(bins, hnInfo, IsProfile<HT>()))) {
0112 return G4Analysis::kInvalidId;
0113 }
0114
0115 Message(G4Analysis::kVL4, "create", GetHnType<HT>(), name);
0116
0117 auto ht = CreateToolsHT(title, bins, hnInfo);
0118
0119
0120 AddAnnotation(ht, hnInfo);
0121
0122
0123 auto info = CreateInformation(name, hnInfo);
0124
0125
0126 auto id = RegisterT(name, ht, info);
0127
0128 Message(G4Analysis::kVL2, "create", GetHnType<HT>(), name);
0129
0130 return id;
0131 }
0132
0133
0134 template <unsigned int DIM, typename HT>
0135 G4bool G4THnToolsManager<DIM, HT>::Set(G4int id,
0136 const std::array<G4HnDimension, DIM>& bins,
0137 const std::array<G4HnDimensionInformation, DIM>& hnInfo)
0138 {
0139
0140 if (! CheckDimensions<DIM>(bins, hnInfo, IsProfile<HT>())) {
0141 return false;
0142 }
0143
0144 auto [ht, info] = GetTHnInFunction(id, "Set" + GetHnType<HT>(), false, false);
0145 if (ht == nullptr) return false;
0146
0147 Message(G4Analysis::kVL4, "configure", GetHnType<HT>(), info->GetName());
0148
0149
0150 ConfigureToolsHT(ht, bins, hnInfo);
0151
0152
0153 AddAnnotation(ht, hnInfo);
0154
0155
0156 UpdateInformation(info, hnInfo);
0157
0158
0159 GetHnManager()->SetActivation(id, true);
0160
0161 return true;
0162 }
0163
0164
0165 template <unsigned int DIM, typename HT>
0166 G4bool G4THnToolsManager<DIM, HT>::Scale(G4int id, G4double factor)
0167 {
0168 auto ht = GetTInFunction(id, "Scale" + GetHnType<HT>(), false, false);
0169 if (ht == nullptr) return false;
0170
0171 return ht->scale(factor);
0172 }
0173
0174
0175 template <unsigned int DIM, typename HT>
0176 G4bool G4THnToolsManager<DIM, HT>::Fill(G4int id,
0177 std::array<G4double, DIM> value, G4double weight)
0178 {
0179 auto [ht, info] = GetTHnInFunction(id, "Fill"+ GetHnType<HT>(), true, false);
0180
0181 if (ht == nullptr) {
0182 Warn("Failed to fill " + GetHnType<HT>() + " id " + std::to_string(id) +
0183 ". Histogram does not exist.", fkClass, "Fill");
0184 return false;
0185 }
0186
0187 if ( G4THnManager<HT>::fState.GetIsActivation() && ( ! info->GetActivation() ) ) {
0188 return false;
0189 }
0190
0191 std::array<G4double, DIM> newValue(value);
0192 auto result = FillHT(ht, *info, newValue, weight);
0193
0194 if ( IsVerbose(G4Analysis::kVL4) ) {
0195
0196 G4String dims("xyz");
0197 G4String message = " id " + to_string(id);
0198 for (unsigned int idim = 0; idim < DIM; ++idim) {
0199 auto xyz = dims.substr(idim,1);
0200 message +=
0201 " " + xyz + " " + to_string(value[idim]) + " " +
0202 xyz + "fcn(" + xyz + "value/" + xyz + "unit) " + to_string(newValue[idim]);
0203 }
0204 message += " weight " + to_string(weight);
0205
0206 Message(G4Analysis::kVL4, "fill", GetHnType<HT>(), message);
0207 }
0208
0209 return result;
0210 }
0211
0212
0213 template <unsigned int DIM, typename HT>
0214 G4int G4THnToolsManager<DIM, HT>::GetId(const G4String& name, G4bool warn) const
0215 {
0216 return GetTId(name, warn);
0217 }
0218
0219
0220 template <unsigned int DIM, typename HT>
0221 G4int G4THnToolsManager<DIM, HT>::GetNofHns(G4bool onlyIfExist) const
0222 {
0223 return G4THnManager<HT>::GetNofHns(onlyIfExist);
0224 }
0225
0226
0227 template <unsigned int DIM, typename HT>
0228 G4int G4THnToolsManager<DIM, HT>::GetNbins(unsigned int idim, G4int id) const
0229 {
0230 auto ht = GetTInFunction(id, "GetNbins");
0231 if (ht == nullptr) return 0;
0232
0233 return ht->get_axis(idim).bins();
0234 }
0235
0236
0237 template <unsigned int DIM, typename HT>
0238 G4double G4THnToolsManager<DIM, HT>::GetMinValue(unsigned int idim, G4int id) const
0239 {
0240 auto ht = GetTInFunction(id, "GetMinValue");
0241 if (ht == nullptr) return 0.;
0242
0243 return ht->get_axis(idim).lower_edge();
0244 }
0245
0246
0247 template <unsigned int DIM, typename HT>
0248 G4double G4THnToolsManager<DIM, HT>::GetMaxValue(unsigned int idim, G4int id) const
0249 {
0250 auto ht = GetTInFunction(id, "GetMaxValue");
0251 if (ht == nullptr) return 0.;
0252
0253 return ht->get_axis(idim).upper_edge();
0254 }
0255
0256
0257 template <unsigned int DIM, typename HT>
0258 G4double G4THnToolsManager<DIM, HT>::GetWidth(unsigned int idim, G4int id) const
0259 {
0260 auto ht = GetTInFunction(id, "GetWidth", true, false);
0261 if (ht == nullptr) return 0.;
0262
0263 auto nbins = ht->get_axis(idim).bins();
0264 if (nbins == 0u) {
0265 Warn("nbins = 0 ! for " + GetHnType<HT>(), fkClass, "GetWidth");
0266 return 0.;
0267 }
0268
0269 return ( ht->get_axis(idim).upper_edge() -
0270 ht->get_axis(idim).lower_edge() )/nbins;
0271 }
0272
0273
0274 template <unsigned int DIM, typename HT>
0275 G4bool G4THnToolsManager<DIM, HT>::SetTitle(G4int id, const G4String& title)
0276 {
0277 auto ht = GetTInFunction(id, "SetTitle");
0278 if (ht == nullptr) return false;
0279
0280 return ht->set_title(title);
0281 }
0282
0283
0284 template <unsigned int DIM, typename HT>
0285 G4bool G4THnToolsManager<DIM, HT>::SetAxisTitle(unsigned int idim, G4int id, const G4String& title)
0286 {
0287 auto ht = GetTInFunction(id, "SetAxisTitle");
0288 if (ht == nullptr) return false;
0289
0290 ht->add_annotation(fkKeyAxisTitle[idim], title);
0291
0292 return true;
0293 }
0294
0295
0296 template <unsigned int DIM, typename HT>
0297 G4String G4THnToolsManager<DIM, HT>::GetTitle(G4int id) const
0298 {
0299 auto ht = GetTInFunction(id, "GetTitle");
0300 if (ht == nullptr) return G4String();
0301
0302 return ht->title();
0303 }
0304
0305
0306
0307 template <unsigned int DIM, typename HT>
0308 G4String G4THnToolsManager<DIM, HT>::GetAxisTitle(unsigned int idim, G4int id) const
0309 {
0310 auto ht = GetTInFunction(id, "GetAxisTitle");
0311 if (ht == nullptr) return G4String();
0312
0313 G4String title;
0314 G4bool result = ht->annotation(fkKeyAxisTitle[idim], title);
0315
0316 if ( ! result ) {
0317 Warn("Got wrong dimension " + to_string(idim) + " for " + GetHnType<HT>(),
0318 fkClass, "GetAxisTitle");
0319
0320 return {};
0321 }
0322
0323 return title;
0324 }
0325
0326
0327 template <unsigned int DIM, typename HT>
0328 G4bool G4THnToolsManager<DIM, HT>::List(std::ostream& output, G4bool onlyIfActive)
0329 {
0330 return G4THnManager<HT>::List(output, onlyIfActive);
0331 }
0332
0333
0334 template <unsigned int DIM, typename HT>
0335 G4bool G4THnToolsManager<DIM, HT>::Delete(G4int id, G4bool keepSetting)
0336 {
0337 G4String message = " id " + to_string(id);
0338 Message(G4Analysis::kVL4, "delete", GetHnType<HT>(), message);
0339
0340 auto result = G4THnManager<HT>::DeleteT(id, keepSetting);
0341
0342 Message(G4Analysis::kVL2, "delete", GetHnType<HT>(), message, result);
0343
0344 return result;
0345 }
0346
0347
0348 template <unsigned int DIM, typename HT>
0349 std::shared_ptr<G4HnManager> G4THnToolsManager<DIM, HT>::GetHnManager() {
0350 return G4THnManager<HT>::fHnManager;
0351 }
0352
0353
0354 template <unsigned int DIM, typename HT>
0355 const std::shared_ptr<G4HnManager> G4THnToolsManager<DIM, HT>::GetHnManager() const
0356 {
0357 return G4THnManager<HT>::fHnManager;
0358 }
0359