Geant4 Cross Reference |
1 // 2 // ******************************************************************** 3 // * License and Disclaimer * 4 // * * 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. * 10 // * * 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitation of liability. * 17 // * * 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************************************** 25 // 26 27 // Author: Ivana Hrivnacova, 10/08/2022 (ivana@ipno.in2p3.fr) 28 29 #include "G4AnalysisUtilities.hh" 30 31 using G4Analysis::kX; 32 using G4Analysis::kY; 33 using G4Analysis::kZ; 34 using G4Analysis::kMaxDim; 35 using G4Analysis::CheckDimensions; 36 using G4Analysis::GetHnType; 37 using G4Analysis::IsProfile; 38 using G4Analysis::Warn; 39 40 //_____________________________________________________________________________ 41 template <unsigned int DIM, typename HT> 42 const std::array<std::string, kMaxDim> G4THnToolsManager<DIM, HT>::fkKeyAxisTitle = 43 { "axis_x.title", "axis_y.title", "axis_z.title" }; 44 45 //_____________________________________________________________________________ 46 template <unsigned int DIM, typename HT> 47 G4THnToolsManager<DIM, HT>::G4THnToolsManager(const G4AnalysisManagerState& state) 48 : G4THnManager<HT>(state) 49 { 50 fMessenger = std::make_unique<G4THnMessenger<DIM, HT>>(this); 51 GetHnManager()->CreateMessenger(); 52 } 53 54 //_____________________________________________________________________________ 55 template <unsigned int DIM, typename HT> 56 void G4THnToolsManager<DIM, HT>::UpdateInformation(G4HnInformation* hnInformation, 57 const std::array<G4HnDimensionInformation, DIM>& hnInfo) 58 { 59 for (unsigned int idim = 0; idim < DIM; ++idim) { 60 hnInformation->SetDimension(idim, hnInfo[idim]); 61 } 62 } 63 64 //_____________________________________________________________________________ 65 template <unsigned int DIM, typename HT> 66 G4HnInformation* G4THnToolsManager<DIM, HT>::CreateInformation(const G4String& name, 67 const std::array<G4HnDimensionInformation, DIM>& hnInfo) 68 { 69 auto hnInformation = new G4HnInformation(name, DIM); 70 for (unsigned int idim = 0; idim < DIM; ++idim) { 71 hnInformation->AddDimension(hnInfo[idim]); 72 } 73 74 return hnInformation; 75 } 76 77 //_____________________________________________________________________________ 78 template <unsigned int DIM, typename HT> 79 void G4THnToolsManager<DIM, HT>::AddAnnotation(HT* ht, 80 const std::array<G4HnDimensionInformation, DIM>& hnInfo) 81 { 82 for (unsigned int idim = 0; idim < DIM; ++idim) { 83 G4String axisTitle; 84 G4Analysis::UpdateTitle(axisTitle, hnInfo[idim]); 85 86 ht->add_annotation(fkKeyAxisTitle[idim], axisTitle); 87 } 88 } 89 90 //_____________________________________________________________________________ 91 template <unsigned int DIM, typename HT> 92 G4bool G4THnToolsManager<DIM, HT>::CheckName(const G4String& name) const 93 { 94 if (name.size() == 0u) { 95 G4Analysis::Warn("Empty " + GetHnType<HT>() + " name is not allowed.\n" + 96 GetHnType<HT>() + " was not created.", fkClass, "CheckName"); 97 return false; 98 } 99 return true; 100 } 101 102 //_____________________________________________________________________________ 103 template <unsigned int DIM, typename HT> 104 G4int G4THnToolsManager<DIM, HT>::Create( 105 const G4String& name, const G4String& title, 106 const std::array<G4HnDimension, DIM>& bins, 107 const std::array<G4HnDimensionInformation, DIM>& hnInfo) 108 { 109 // Check parameters 110 if ((! CheckName(name)) || 111 (! CheckDimensions<DIM>(bins, hnInfo, IsProfile<HT>()))) { 112 return G4Analysis::kInvalidId; 113 } 114 115 Message(G4Analysis::kVL4, "create", GetHnType<HT>(), name); 116 117 auto ht = CreateToolsHT(title, bins, hnInfo); 118 119 // Add annotation 120 AddAnnotation(ht, hnInfo); 121 122 // Create Hn information 123 auto info = CreateInformation(name, hnInfo); 124 125 // Register histogram & info 126 auto id = RegisterT(name, ht, info); 127 128 Message(G4Analysis::kVL2, "create", GetHnType<HT>(), name); 129 130 return id; 131 } 132 133 //_____________________________________________________________________________ 134 template <unsigned int DIM, typename HT> 135 G4bool G4THnToolsManager<DIM, HT>::Set(G4int id, 136 const std::array<G4HnDimension, DIM>& bins, 137 const std::array<G4HnDimensionInformation, DIM>& hnInfo) 138 { 139 // Check parameters 140 if (! CheckDimensions<DIM>(bins, hnInfo, IsProfile<HT>())) { 141 return false; 142 } 143 144 auto [ht, info] = GetTHnInFunction(id, "Set" + GetHnType<HT>(), false, false); 145 if (ht == nullptr) return false; 146 147 Message(G4Analysis::kVL4, "configure", GetHnType<HT>(), info->GetName()); 148 149 // Configure tools h2 150 ConfigureToolsHT(ht, bins, hnInfo); 151 152 // Add annotation 153 AddAnnotation(ht, hnInfo); 154 155 // Update information 156 UpdateInformation(info, hnInfo); 157 158 // Set activation 159 GetHnManager()->SetActivation(id, true); 160 161 return true; 162 } 163 164 //_____________________________________________________________________________ 165 template <unsigned int DIM, typename HT> 166 G4bool G4THnToolsManager<DIM, HT>::Scale(G4int id, G4double factor) 167 { 168 auto ht = GetTInFunction(id, "Scale" + GetHnType<HT>(), false, false); 169 if (ht == nullptr) return false; 170 171 return ht->scale(factor); 172 } 173 174 //_____________________________________________________________________________ 175 template <unsigned int DIM, typename HT> 176 G4bool G4THnToolsManager<DIM, HT>::Fill(G4int id, 177 std::array<G4double, DIM> value, G4double weight) 178 { 179 auto [ht, info] = GetTHnInFunction(id, "Fill"+ GetHnType<HT>(), true, false); 180 181 if (ht == nullptr) { 182 Warn("Failed to fill " + GetHnType<HT>() + " id " + std::to_string(id) + 183 ". Histogram does not exist.", fkClass, "Fill"); 184 return false; 185 } 186 187 if ( G4THnManager<HT>::fState.GetIsActivation() && ( ! info->GetActivation() ) ) { 188 return false; 189 } 190 191 std::array<G4double, DIM> newValue(value); 192 auto result = FillHT(ht, *info, newValue, weight); 193 194 if ( IsVerbose(G4Analysis::kVL4) ) { 195 // compose message 196 G4String dims("xyz"); 197 G4String message = " id " + to_string(id); 198 for (unsigned int idim = 0; idim < DIM; ++idim) { 199 auto xyz = dims.substr(idim,1); 200 message += 201 " " + xyz + " " + to_string(value[idim]) + " " + 202 xyz + "fcn(" + xyz + "value/" + xyz + "unit) " + to_string(newValue[idim]); 203 } 204 message += " weight " + to_string(weight); 205 206 Message(G4Analysis::kVL4, "fill", GetHnType<HT>(), message); 207 } 208 209 return result; 210 } 211 212 //_____________________________________________________________________________ 213 template <unsigned int DIM, typename HT> 214 G4int G4THnToolsManager<DIM, HT>::GetId(const G4String& name, G4bool warn) const 215 { 216 return GetTId(name, warn); 217 } 218 219 //_____________________________________________________________________________ 220 template <unsigned int DIM, typename HT> 221 G4int G4THnToolsManager<DIM, HT>::GetNofHns(G4bool onlyIfExist) const 222 { 223 return G4THnManager<HT>::GetNofHns(onlyIfExist); 224 } 225 226 //_____________________________________________________________________________ 227 template <unsigned int DIM, typename HT> 228 G4int G4THnToolsManager<DIM, HT>::GetNbins(unsigned int idim, G4int id) const 229 { 230 auto ht = GetTInFunction(id, "GetNbins"); 231 if (ht == nullptr) return 0; 232 233 return ht->get_axis(idim).bins(); 234 } 235 236 //_____________________________________________________________________________ 237 template <unsigned int DIM, typename HT> 238 G4double G4THnToolsManager<DIM, HT>::GetMinValue(unsigned int idim, G4int id) const 239 { 240 auto ht = GetTInFunction(id, "GetMinValue"); 241 if (ht == nullptr) return 0.; 242 243 return ht->get_axis(idim).lower_edge(); 244 } 245 246 //_____________________________________________________________________________ 247 template <unsigned int DIM, typename HT> 248 G4double G4THnToolsManager<DIM, HT>::GetMaxValue(unsigned int idim, G4int id) const 249 { 250 auto ht = GetTInFunction(id, "GetMaxValue"); 251 if (ht == nullptr) return 0.; 252 253 return ht->get_axis(idim).upper_edge(); 254 } 255 256 //_____________________________________________________________________________ 257 template <unsigned int DIM, typename HT> 258 G4double G4THnToolsManager<DIM, HT>::GetWidth(unsigned int idim, G4int id) const 259 { 260 auto ht = GetTInFunction(id, "GetWidth", true, false); 261 if (ht == nullptr) return 0.; 262 263 auto nbins = ht->get_axis(idim).bins(); 264 if (nbins == 0u) { 265 Warn("nbins = 0 ! for " + GetHnType<HT>(), fkClass, "GetWidth"); 266 return 0.; 267 } 268 269 return ( ht->get_axis(idim).upper_edge() - 270 ht->get_axis(idim).lower_edge() )/nbins; 271 } 272 273 //_____________________________________________________________________________ 274 template <unsigned int DIM, typename HT> 275 G4bool G4THnToolsManager<DIM, HT>::SetTitle(G4int id, const G4String& title) 276 { 277 auto ht = GetTInFunction(id, "SetTitle"); 278 if (ht == nullptr) return false; 279 280 return ht->set_title(title); 281 } 282 283 //_____________________________________________________________________________ 284 template <unsigned int DIM, typename HT> 285 G4bool G4THnToolsManager<DIM, HT>::SetAxisTitle(unsigned int idim, G4int id, const G4String& title) 286 { 287 auto ht = GetTInFunction(id, "SetAxisTitle"); 288 if (ht == nullptr) return false; 289 290 ht->add_annotation(fkKeyAxisTitle[idim], title); 291 292 return true; 293 } 294 295 //_____________________________________________________________________________ 296 template <unsigned int DIM, typename HT> 297 G4String G4THnToolsManager<DIM, HT>::GetTitle(G4int id) const 298 { 299 auto ht = GetTInFunction(id, "GetTitle"); 300 if (ht == nullptr) return G4String(); 301 302 return ht->title(); 303 } 304 305 306 //_____________________________________________________________________________ 307 template <unsigned int DIM, typename HT> 308 G4String G4THnToolsManager<DIM, HT>::GetAxisTitle(unsigned int idim, G4int id) const 309 { 310 auto ht = GetTInFunction(id, "GetAxisTitle"); 311 if (ht == nullptr) return G4String(); 312 313 G4String title; 314 G4bool result = ht->annotation(fkKeyAxisTitle[idim], title); 315 316 if ( ! result ) { 317 Warn("Got wrong dimension " + to_string(idim) + " for " + GetHnType<HT>(), 318 fkClass, "GetAxisTitle"); 319 320 return {}; 321 } 322 323 return title; 324 } 325 326 //_____________________________________________________________________________ 327 template <unsigned int DIM, typename HT> 328 G4bool G4THnToolsManager<DIM, HT>::List(std::ostream& output, G4bool onlyIfActive) 329 { 330 return G4THnManager<HT>::List(output, onlyIfActive); 331 } 332 333 //_____________________________________________________________________________ 334 template <unsigned int DIM, typename HT> 335 G4bool G4THnToolsManager<DIM, HT>::Delete(G4int id, G4bool keepSetting) 336 { 337 G4String message = " id " + to_string(id); 338 Message(G4Analysis::kVL4, "delete", GetHnType<HT>(), message); 339 340 auto result = G4THnManager<HT>::DeleteT(id, keepSetting); 341 342 Message(G4Analysis::kVL2, "delete", GetHnType<HT>(), message, result); 343 344 return result; 345 } 346 347 //_____________________________________________________________________________ 348 template <unsigned int DIM, typename HT> 349 std::shared_ptr<G4HnManager> G4THnToolsManager<DIM, HT>::GetHnManager() { 350 return G4THnManager<HT>::fHnManager; 351 } 352 353 //_____________________________________________________________________________ 354 template <unsigned int DIM, typename HT> 355 const std::shared_ptr<G4HnManager> G4THnToolsManager<DIM, HT>::GetHnManager() const 356 { 357 return G4THnManager<HT>::fHnManager; 358 } 359 360