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, 23/06/2015 (ivana@ipno.in2p3.fr) 28 29 #include "G4AnalysisManagerState.hh" 30 #include "G4HnManager.hh" 31 #include "G4AnalysisUtilities.hh" 32 #include "G4AutoLock.hh" 33 34 #include <iomanip> 35 36 using std::to_string; 37 38 //_____________________________________________________________________________ 39 template <typename HT> 40 G4THnManager<HT>::G4THnManager(const G4AnalysisManagerState& state) 41 : fState(state) 42 { 43 fHnManager = std::make_shared<G4HnManager>(G4Analysis::GetHnType<HT>(), state); 44 } 45 46 //_____________________________________________________________________________ 47 template <typename HT> 48 G4THnManager<HT>::~G4THnManager() 49 { 50 for ( auto t : fTVector ) { 51 delete t; 52 } 53 } 54 55 // 56 // protected methods 57 // 58 59 //_____________________________________________________________________________ 60 template <typename HT> 61 std::pair<HT*, G4HnInformation*> G4THnManager<HT>::GetTHnInFunction( 62 G4int id, std::string_view functionName, G4bool warn, G4bool onlyIfActive) const 63 { 64 G4int index = id - fHnManager->GetFirstId(); 65 if ( index < 0 || index >= G4int(fTHnVector.size()) ) { 66 if ( warn) { 67 G4Analysis::Warn("Histogram " + to_string(id) + " does not exist.", 68 fkClass, functionName); 69 } 70 return {nullptr, nullptr}; 71 } 72 73 // Do not return histogram if inactive 74 if ( fState.GetIsActivation() && onlyIfActive && ( ! fHnManager->GetActivation(id) ) ) { 75 return {nullptr, nullptr}; 76 } 77 78 return fTHnVector[index]; 79 } 80 81 //_____________________________________________________________________________ 82 template <typename HT> 83 HT* G4THnManager<HT>::GetTInFunction( 84 G4int id, std::string_view functionName, G4bool warn, G4bool onlyIfActive) const 85 { 86 return GetTHnInFunction(id, functionName, warn, onlyIfActive).first; 87 } 88 89 //_____________________________________________________________________________ 90 template <typename HT> 91 G4int G4THnManager<HT>::GetTId(const G4String& name, G4bool warn) const 92 { 93 auto it = fNameIdMap.find(name); 94 if ( it == fNameIdMap.end() ) { 95 if ( warn) { 96 G4Analysis::Warn("histogram " + name + " does not exist.", 97 fkClass, "GetTId"); 98 } 99 return G4Analysis::kInvalidId; 100 } 101 return it->second; 102 } 103 104 //_____________________________________________________________________________ 105 template <typename HT> 106 G4bool G4THnManager<HT>::IsVerbose(G4int verboseLevel) const 107 { 108 return fState.IsVerbose(verboseLevel); 109 } 110 111 //_____________________________________________________________________________ 112 template <typename HT> 113 void G4THnManager<HT>::Message( 114 G4int level, const G4String& action, const G4String& objectType, 115 const G4String& objectName, G4bool success) const 116 { 117 fState.Message(level, action, objectType, objectName, success); 118 } 119 120 //_____________________________________________________________________________ 121 template <typename HT> 122 void G4THnManager<HT>::AddTVector(const std::vector<HT*>& tVector) 123 { 124 Message(G4Analysis::kVL4, "merge", "all " + fHnManager->GetHnType()); 125 126 // std::vector<tools::histo::h1d*>::const_iterator itw = h1Vector.begin(); 127 // std::vector<tools::histo::h1d*>::iterator it; 128 // for (it = fH1Vector.begin(); it != fH1Vector.end(); it++ ) { 129 // (*it)->add(*(*itw++)); 130 // } 131 auto itw = tVector.begin(); 132 for ( auto t : fTVector ) { 133 // skip deleted histograms 134 if (t == nullptr) { 135 itw++; 136 continue; 137 } 138 t->add(*(*itw)); 139 (*itw++)->reset(); 140 } 141 142 Message(G4Analysis::kVL2, "merge", "all " + fHnManager->GetHnType()); 143 } 144 145 //_____________________________________________________________________________ 146 template <typename HT> 147 void G4THnManager<HT>::Merge( 148 G4Mutex& mergeMutex, G4THnManager<HT>* masterInstance) 149 { 150 G4AutoLock lH1(&mergeMutex); 151 masterInstance->AddTVector(fTVector); 152 lH1.unlock(); 153 } 154 155 //_____________________________________________________________________________ 156 template <typename HT> 157 typename std::vector<HT*>::iterator G4THnManager<HT>::BeginT() 158 { 159 return fTVector.begin(); 160 } 161 162 //_____________________________________________________________________________ 163 template <typename HT> 164 typename std::vector<HT*>::iterator G4THnManager<HT>::EndT() 165 { 166 return fTVector.end(); 167 } 168 169 //_____________________________________________________________________________ 170 template <typename HT> 171 typename std::vector<HT*>::const_iterator G4THnManager<HT>::BeginConstT() const 172 { 173 return fTVector.begin(); 174 } 175 176 //_____________________________________________________________________________ 177 template <typename HT> 178 typename std::vector<HT*>::const_iterator G4THnManager<HT>::EndConstT() const 179 { 180 return fTVector.end(); 181 } 182 183 // 184 // public methods 185 // 186 187 //_____________________________________________________________________________ 188 template <typename HT> 189 G4int G4THnManager<HT>::RegisterT(const G4String& name, HT* ht, G4HnInformation* info) 190 { 191 G4int index = 0; 192 if (fFreeIds.empty()) { 193 index = (G4int)fTVector.size(); 194 fTVector.push_back(ht); 195 fTHnVector.push_back({ht, info}); 196 fHnManager->AddHnInformation(info); 197 } 198 else { 199 // Get the first freed Id 200 index = *(fFreeIds.begin()) - fHnManager->GetFirstId(); 201 202 // Update vectors at the first freed Id position 203 fTVector[index] = ht; 204 205 // Register new information at the freed Id position 206 fHnManager->AddHnInformation(info, index); 207 208 // Register new histo & inforrmation at the freed Id position 209 fTHnVector[index] = {ht, info}; 210 211 // Remove the id from the set 212 fFreeIds.erase(fFreeIds.begin()); 213 } 214 215 fHnManager->SetLockFirstId(true); 216 fNameIdMap[name] = index + fHnManager->GetFirstId(); 217 return index + fHnManager->GetFirstId(); 218 } 219 220 //_____________________________________________________________________________ 221 template <typename HT> 222 G4bool G4THnManager<HT>::Reset() 223 { 224 // Reset histograms 225 226 auto result = true; 227 228 for ( auto t : fTVector ) { 229 // skip deleted histograms 230 if ( t == nullptr) continue; 231 result &= t->reset(); 232 } 233 234 return result; 235 } 236 237 //_____________________________________________________________________________ 238 template <typename HT> 239 void 240 G4THnManager<HT>::ClearData() 241 { 242 for ( auto t : fTVector ) { 243 delete t; 244 } 245 246 fTVector.clear(); 247 fTHnVector.clear(); 248 fNameIdMap.clear(); 249 250 if ( fHnManager != nullptr ) { 251 fHnManager->ClearData(); 252 } 253 254 Message(G4Analysis::kVL2, "clear", G4Analysis::GetHnType<HT>()); 255 } 256 257 //_____________________________________________________________________________ 258 template <typename HT> 259 G4bool 260 G4THnManager<HT>::DeleteT(G4int id, G4bool keepSetting) 261 { 262 auto [ht, info] = GetTHnInFunction(id, "Delete", true, false); 263 264 if (ht == nullptr) return false; 265 266 // Delete histogram and update vectors 267 delete ht; 268 auto index = id - fHnManager->GetFirstId(); 269 fTVector[index] = nullptr; 270 fTHnVector[index] = std::make_pair(nullptr, info); 271 272 // Update information 273 fHnManager->SetHnDeleted(info, keepSetting); 274 275 // Register freed Id 276 fFreeIds.insert(id); 277 278 return true; 279 } 280 281 //_____________________________________________________________________________ 282 template <typename HT> 283 G4bool G4THnManager<HT>::IsEmpty() const 284 { 285 return ! fTVector.size(); 286 } 287 288 //_____________________________________________________________________________ 289 template <typename HT> 290 HT* G4THnManager<HT>::GetT(G4int id, G4bool warn, G4bool onlyIfActive) const 291 { 292 return GetTInFunction(id, "GetT", warn, onlyIfActive); 293 } 294 295 //_____________________________________________________________________________ 296 template <typename HT> 297 std::vector<HT*>* G4THnManager<HT>::GetTVector() 298 { 299 return &fTVector; 300 } 301 302 //_____________________________________________________________________________ 303 template <typename HT> 304 const std::vector<HT*>& G4THnManager<HT>::GetTVectorRef() const 305 { 306 return fTVector; 307 } 308 309 //_____________________________________________________________________________ 310 template <typename HT> 311 std::vector<std::pair<HT*, G4HnInformation*>>* G4THnManager<HT>::GetTHnVector() 312 { 313 return &fTHnVector; 314 } 315 316 //_____________________________________________________________________________ 317 template <typename HT> 318 const std::vector<std::pair<HT*, G4HnInformation*>>& G4THnManager<HT>::GetTHnVectorRef() const 319 { 320 return fTHnVector; 321 } 322 323 //_____________________________________________________________________________ 324 template <typename HT> 325 G4int G4THnManager<HT>::GetNofHns(G4bool onlyIfExist) const 326 { 327 328 return (onlyIfExist) ? G4int(fTVector.size() - fFreeIds.size()) 329 : G4int(fTVector.size()); 330 } 331 332 //_____________________________________________________________________________ 333 template <typename HT> 334 G4bool G4THnManager<HT>::List(std::ostream& output, G4bool onlyIfActive) const 335 { 336 // Save current output stream formatting 337 std::ios_base::fmtflags outputFlags(output.flags() ); 338 339 // List general info 340 output << fHnManager->GetHnType() << ": " << fHnManager->GetNofActiveHns() << " active "; 341 if (! onlyIfActive) { 342 output << " of " << GetNofHns(true) << " defined "; 343 } 344 output << G4endl; 345 346 // Define optimal field widths 347 size_t maxNameLength = 0; 348 size_t maxTitleLength = 0; 349 size_t maxEntries = 0; 350 for ( const auto& [ht, hnInfo] : fTHnVector) { 351 // skip deleletd objects 352 if (ht == nullptr) continue; 353 if (hnInfo->GetName().length() > maxNameLength) { 354 maxNameLength = hnInfo->GetName().length(); 355 } 356 if (ht->title().length() > maxTitleLength) { 357 maxTitleLength = ht->title().length(); 358 } 359 if (ht->entries() > maxEntries) { 360 maxEntries = ht->entries(); 361 } 362 } 363 size_t maxIdWidth = std::to_string(fTVector.size() + fHnManager->GetFirstId()).length(); 364 size_t maxEntriesWidth = std::to_string(maxEntries).length(); 365 // update string width for adde double quotas 366 maxNameLength += 2; 367 maxTitleLength += 2; 368 369 // List objects 370 auto id = fHnManager->GetFirstId(); 371 for (const auto& [ht, hnInfo] : fTHnVector) { 372 // skip deleted or inactivated objects 373 if ( (fState.GetIsActivation() && onlyIfActive && (! hnInfo->GetActivation())) || 374 (hnInfo->GetDeleted()) ) { 375 id++; 376 continue; 377 } 378 379 // print selected info 380 output << " id: " << std::setw((G4int)maxIdWidth) << id++ 381 << " name: \"" << std::setw((G4int)maxNameLength) << std::left << hnInfo->GetName() + "\"" 382 << " title: \"" << std::setw((G4int)maxTitleLength) << std::left << ht->title() + "\"" 383 << " entries: " << std::setw((G4int)maxEntriesWidth) << ht->entries(); 384 if (! onlyIfActive) { 385 output << " active: " << std::boolalpha << hnInfo->GetActivation(); 386 } 387 output << G4endl; 388 } 389 390 // Restore the output stream formatting 391 output.flags(outputFlags); 392 393 return output.good(); 394 } 395