Warning, file /include/Geant4/G4THnManager.icc was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
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 "G4AnalysisManagerState.hh"
0030 #include "G4HnManager.hh"
0031 #include "G4AnalysisUtilities.hh"
0032 #include "G4AutoLock.hh"
0033
0034 #include <iomanip>
0035
0036 using std::to_string;
0037
0038
0039 template <typename HT>
0040 G4THnManager<HT>::G4THnManager(const G4AnalysisManagerState& state)
0041 : fState(state)
0042 {
0043 fHnManager = std::make_shared<G4HnManager>(G4Analysis::GetHnType<HT>(), state);
0044 }
0045
0046
0047 template <typename HT>
0048 G4THnManager<HT>::~G4THnManager()
0049 {
0050 for ( auto t : fTVector ) {
0051 delete t;
0052 }
0053 }
0054
0055
0056
0057
0058
0059
0060 template <typename HT>
0061 std::pair<HT*, G4HnInformation*> G4THnManager<HT>::GetTHnInFunction(
0062 G4int id, std::string_view functionName, G4bool warn, G4bool onlyIfActive) const
0063 {
0064 G4int index = id - fHnManager->GetFirstId();
0065 if ( index < 0 || index >= G4int(fTHnVector.size()) ) {
0066 if ( warn) {
0067 G4Analysis::Warn("Histogram " + to_string(id) + " does not exist.",
0068 fkClass, functionName);
0069 }
0070 return {nullptr, nullptr};
0071 }
0072
0073
0074 if ( fState.GetIsActivation() && onlyIfActive && ( ! fHnManager->GetActivation(id) ) ) {
0075 return {nullptr, nullptr};
0076 }
0077
0078 return fTHnVector[index];
0079 }
0080
0081
0082 template <typename HT>
0083 HT* G4THnManager<HT>::GetTInFunction(
0084 G4int id, std::string_view functionName, G4bool warn, G4bool onlyIfActive) const
0085 {
0086 return GetTHnInFunction(id, functionName, warn, onlyIfActive).first;
0087 }
0088
0089
0090 template <typename HT>
0091 G4int G4THnManager<HT>::GetTId(const G4String& name, G4bool warn) const
0092 {
0093 auto it = fNameIdMap.find(name);
0094 if ( it == fNameIdMap.end() ) {
0095 if ( warn) {
0096 G4Analysis::Warn("histogram " + name + " does not exist.",
0097 fkClass, "GetTId");
0098 }
0099 return G4Analysis::kInvalidId;
0100 }
0101 return it->second;
0102 }
0103
0104
0105 template <typename HT>
0106 G4bool G4THnManager<HT>::IsVerbose(G4int verboseLevel) const
0107 {
0108 return fState.IsVerbose(verboseLevel);
0109 }
0110
0111
0112 template <typename HT>
0113 void G4THnManager<HT>::Message(
0114 G4int level, const G4String& action, const G4String& objectType,
0115 const G4String& objectName, G4bool success) const
0116 {
0117 fState.Message(level, action, objectType, objectName, success);
0118 }
0119
0120
0121 template <typename HT>
0122 void G4THnManager<HT>::AddTVector(const std::vector<HT*>& tVector)
0123 {
0124 Message(G4Analysis::kVL4, "merge", "all " + fHnManager->GetHnType());
0125
0126
0127
0128
0129
0130
0131 auto itw = tVector.begin();
0132 for ( auto t : fTVector ) {
0133
0134 if (t == nullptr) {
0135 itw++;
0136 continue;
0137 }
0138 t->add(*(*itw));
0139 (*itw++)->reset();
0140 }
0141
0142 Message(G4Analysis::kVL2, "merge", "all " + fHnManager->GetHnType());
0143 }
0144
0145
0146 template <typename HT>
0147 void G4THnManager<HT>::Merge(
0148 G4Mutex& mergeMutex, G4THnManager<HT>* masterInstance)
0149 {
0150 G4AutoLock lH1(&mergeMutex);
0151 masterInstance->AddTVector(fTVector);
0152 lH1.unlock();
0153 }
0154
0155
0156 template <typename HT>
0157 typename std::vector<HT*>::iterator G4THnManager<HT>::BeginT()
0158 {
0159 return fTVector.begin();
0160 }
0161
0162
0163 template <typename HT>
0164 typename std::vector<HT*>::iterator G4THnManager<HT>::EndT()
0165 {
0166 return fTVector.end();
0167 }
0168
0169
0170 template <typename HT>
0171 typename std::vector<HT*>::const_iterator G4THnManager<HT>::BeginConstT() const
0172 {
0173 return fTVector.begin();
0174 }
0175
0176
0177 template <typename HT>
0178 typename std::vector<HT*>::const_iterator G4THnManager<HT>::EndConstT() const
0179 {
0180 return fTVector.end();
0181 }
0182
0183
0184
0185
0186
0187
0188 template <typename HT>
0189 G4int G4THnManager<HT>::RegisterT(const G4String& name, HT* ht, G4HnInformation* info)
0190 {
0191 G4int index = 0;
0192 if (fFreeIds.empty()) {
0193 index = (G4int)fTVector.size();
0194 fTVector.push_back(ht);
0195 fTHnVector.push_back({ht, info});
0196 fHnManager->AddHnInformation(info);
0197 }
0198 else {
0199
0200 index = *(fFreeIds.begin()) - fHnManager->GetFirstId();
0201
0202
0203 fTVector[index] = ht;
0204
0205
0206 fHnManager->AddHnInformation(info, index);
0207
0208
0209 fTHnVector[index] = {ht, info};
0210
0211
0212 fFreeIds.erase(fFreeIds.begin());
0213 }
0214
0215 fHnManager->SetLockFirstId(true);
0216 fNameIdMap[name] = index + fHnManager->GetFirstId();
0217 return index + fHnManager->GetFirstId();
0218 }
0219
0220
0221 template <typename HT>
0222 G4bool G4THnManager<HT>::Reset()
0223 {
0224
0225
0226 auto result = true;
0227
0228 for ( auto t : fTVector ) {
0229
0230 if ( t == nullptr) continue;
0231 result &= t->reset();
0232 }
0233
0234 return result;
0235 }
0236
0237
0238 template <typename HT>
0239 void
0240 G4THnManager<HT>::ClearData()
0241 {
0242 for ( auto t : fTVector ) {
0243 delete t;
0244 }
0245
0246 fTVector.clear();
0247 fTHnVector.clear();
0248 fNameIdMap.clear();
0249
0250 if ( fHnManager != nullptr ) {
0251 fHnManager->ClearData();
0252 }
0253
0254 Message(G4Analysis::kVL2, "clear", G4Analysis::GetHnType<HT>());
0255 }
0256
0257
0258 template <typename HT>
0259 G4bool
0260 G4THnManager<HT>::DeleteT(G4int id, G4bool keepSetting)
0261 {
0262 auto [ht, info] = GetTHnInFunction(id, "Delete", true, false);
0263
0264 if (ht == nullptr) return false;
0265
0266
0267 delete ht;
0268 auto index = id - fHnManager->GetFirstId();
0269 fTVector[index] = nullptr;
0270 fTHnVector[index] = std::make_pair(nullptr, info);
0271
0272
0273 fHnManager->SetHnDeleted(info, keepSetting);
0274
0275
0276 fFreeIds.insert(id);
0277
0278 return true;
0279 }
0280
0281
0282 template <typename HT>
0283 G4bool G4THnManager<HT>::IsEmpty() const
0284 {
0285 return ! fTVector.size();
0286 }
0287
0288
0289 template <typename HT>
0290 HT* G4THnManager<HT>::GetT(G4int id, G4bool warn, G4bool onlyIfActive) const
0291 {
0292 return GetTInFunction(id, "GetT", warn, onlyIfActive);
0293 }
0294
0295
0296 template <typename HT>
0297 std::vector<HT*>* G4THnManager<HT>::GetTVector()
0298 {
0299 return &fTVector;
0300 }
0301
0302
0303 template <typename HT>
0304 const std::vector<HT*>& G4THnManager<HT>::GetTVectorRef() const
0305 {
0306 return fTVector;
0307 }
0308
0309
0310 template <typename HT>
0311 std::vector<std::pair<HT*, G4HnInformation*>>* G4THnManager<HT>::GetTHnVector()
0312 {
0313 return &fTHnVector;
0314 }
0315
0316
0317 template <typename HT>
0318 const std::vector<std::pair<HT*, G4HnInformation*>>& G4THnManager<HT>::GetTHnVectorRef() const
0319 {
0320 return fTHnVector;
0321 }
0322
0323
0324 template <typename HT>
0325 G4int G4THnManager<HT>::GetNofHns(G4bool onlyIfExist) const
0326 {
0327
0328 return (onlyIfExist) ? G4int(fTVector.size() - fFreeIds.size())
0329 : G4int(fTVector.size());
0330 }
0331
0332
0333 template <typename HT>
0334 G4bool G4THnManager<HT>::List(std::ostream& output, G4bool onlyIfActive) const
0335 {
0336
0337 std::ios_base::fmtflags outputFlags(output.flags() );
0338
0339
0340 output << fHnManager->GetHnType() << ": " << fHnManager->GetNofActiveHns() << " active ";
0341 if (! onlyIfActive) {
0342 output << " of " << GetNofHns(true) << " defined ";
0343 }
0344 output << G4endl;
0345
0346
0347 size_t maxNameLength = 0;
0348 size_t maxTitleLength = 0;
0349 size_t maxEntries = 0;
0350 for ( const auto& [ht, hnInfo] : fTHnVector) {
0351
0352 if (ht == nullptr) continue;
0353 if (hnInfo->GetName().length() > maxNameLength) {
0354 maxNameLength = hnInfo->GetName().length();
0355 }
0356 if (ht->title().length() > maxTitleLength) {
0357 maxTitleLength = ht->title().length();
0358 }
0359 if (ht->entries() > maxEntries) {
0360 maxEntries = ht->entries();
0361 }
0362 }
0363 size_t maxIdWidth = std::to_string(fTVector.size() + fHnManager->GetFirstId()).length();
0364 size_t maxEntriesWidth = std::to_string(maxEntries).length();
0365
0366 maxNameLength += 2;
0367 maxTitleLength += 2;
0368
0369
0370 auto id = fHnManager->GetFirstId();
0371 for (const auto& [ht, hnInfo] : fTHnVector) {
0372
0373 if ( (fState.GetIsActivation() && onlyIfActive && (! hnInfo->GetActivation())) ||
0374 (hnInfo->GetDeleted()) ) {
0375 id++;
0376 continue;
0377 }
0378
0379
0380 output << " id: " << std::setw((G4int)maxIdWidth) << id++
0381 << " name: \"" << std::setw((G4int)maxNameLength) << std::left << hnInfo->GetName() + "\""
0382 << " title: \"" << std::setw((G4int)maxTitleLength) << std::left << ht->title() + "\""
0383 << " entries: " << std::setw((G4int)maxEntriesWidth) << ht->entries();
0384 if (! onlyIfActive) {
0385 output << " active: " << std::boolalpha << hnInfo->GetActivation();
0386 }
0387 output << G4endl;
0388 }
0389
0390
0391 output.flags(outputFlags);
0392
0393 return output.good();
0394 }